How to upgrade the :update=>'div_id' option of remote_form_for from Rails 2 to Rails 3?

前端 未结 3 1448
无人及你
无人及你 2021-02-06 18:09

I can\'t figure out how to upgrade this code from Rails 2 to Rails 3:

<% remote_form_for(item, :update => \'div_id\') do |f| %>
... 

I

3条回答
  •  醉酒成梦
    2021-02-06 19:01

    You could use RJS, but that's being deprecated too (and for good reason). The simplified, best-practices way to handle this in Rails 3+ is as follows (assuming jQuery):

    # your_view.html.erb
    
    <%= form_for :some_model, :remote => true, :id => 'form-id' do |f| %> ... <% end %> # application.js (or any other .js loaded on the page) $(function(){ $('#form-id').bind('ajax:success', function(xhr, data, status){ $('#receiver-id').html(data); }); });

    The ajax:success hook gets called by the jquery-ujs (aka jquery-rails, aka rails-ujs) remote link/form handler. See for yourself. There are lots of other callbacks/hooks available for you to use, too. If you wanted to make this even more flexible, you could use live instead of bind, and bind to a class that dictates where the ouput goes (e.g. "sidebar") and then all remote links/forms with the sidebar class would have their HTML response go to div#sidebar.

提交回复
热议问题