Onchange remote_function in rails 3 - is it really? how can i do it unobtrusively?

I have two questions.

I have a select tag in a rails 3 application that looks like this:

 <%= select(@adverse_event_template_settings, 
    "display_arms", 
    options_for_select([["Yes", true], ["No", false]]), 
    { :selected => :display_arms },  
    :onchange => remote_function(:update => "display_arms", 
          :method => "put", 
          :with => "'display_arms=' + value", 
          :url => { :controller => :adverse_event_template_settings, 
               :action => :update, :id => @aets.id})) %>

This does what I want to do, i.e. invokes a controller action when the value of the selection field changes.

Despite the fact that this works, I want to know: 1) Is it really in rails 3? I know this is obsessive javascript, but the remote_function working in rails 3 seems strange to me - is there something wrong here?

and if the above is NOT valid, I really would like to know 2) how can I perform the same action (i.e., invoke the same action on the same controller) is unobtrusive? Is there a way to make this simple and clean on rails 3? I desperately need help coding this part.

! , .

+5
1

( , jQuery).

javascript .js onchange jQuery.

<%= select(@adverse_event_template_settings, 
    "display_arms", 
    options_for_select([["Yes", true], ["No", false]]), 
    { :selected => :display_arms }) %>

js:

  $(document).ready(function() {

    $('#display_arms').change(function(){
      $.ajax({url: 'YOUR URL',
        data: 'display_arms=' + value,
        success: function(data){
          $('#display_arms').html(data);
          }
      })
    });
  });

( , )

, , ( !).

+7

All Articles