As Sergei said: There is no remote_function in rails 3.
The solution is to unobtrusively add the onchange handler using something like:
$(function($) {
$("#selector_for_element").change(function() {
$.ajax({url: '<%= url_for :action => 'action_name', :id => @model.id %>',
data: 'selected=' + this.value,
dataType: 'script'})
});
});
This will call the action_name
method in the current controller, passing the result of selected=...
as a parameter. Now you can return a piece of javascript, for instance using something like:
calculated = calculate_some_value_using(params[:selected])
render(:update) {|page|
page << "$('#selector_for_field_to_update).html(#{calculated})"
}
in the controller. The string you append to ''page'' should be valid javascript and will be executed automatically if you use $.ajax.
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:
<%= select_tag :category_id,
options_for_select(
Category.find(:all,:select=>"name,id").collect{|c| [c.name,c.id]}),
:data => { :remote => true, :url => url_for(:controller => "posts",
:action => "filter_post",
:filter =>"category") } %>
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.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…