Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>As Sergei said: There is no remote_function in rails 3.</p> <p>The solution is to unobtrusively add the onchange handler using something like:</p> <pre><code>$(function($) { $("#selector_for_element").change(function() { $.ajax({url: '&lt;%= url_for :action =&gt; 'action_name', :id =&gt; @model.id %&gt;', data: 'selected=' + this.value, dataType: 'script'}) }); }); </code></pre> <p>This will call the <code>action_name</code> method in the current controller, passing the result of <code>selected=...</code> as a parameter. Now you can return a piece of javascript, for instance using something like:</p> <pre><code>calculated = calculate_some_value_using(params[:selected]) render(:update) {|page| page &lt;&lt; "$('#selector_for_field_to_update).html(#{calculated})" } </code></pre> <p>in the controller. The string you append to ''page'' should be valid javascript and will be executed automatically if you use $.ajax. </p> <p>EDIT: FYI, we recently added a select change helper to jquery-ujs, so you can do this unobtrusive way using the built-in jquery-ujs adapter now:</p> <pre><code>&lt;%= select_tag :category_id, options_for_select( Category.find(:all,:select=&gt;"name,id").collect{|c| [c.name,c.id]}), :data =&gt; { :remote =&gt; true, :url =&gt; url_for(:controller =&gt; "posts", :action =&gt; "filter_post", :filter =&gt;"category") } %&gt; </code></pre> <p>Though to be fair, adding a select change event really isn't ever going to be "unobtrusive" as there is no graceful fallback for users with javascript disabled unless your form doesn't depend on the result of the AJAX request.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload