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

前端 未结 3 1443
无人及你
无人及你 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 18:54

    I know the question is old but I when migrating to Rails 3 I found a pretty good way of doing this, so I thought I would post it here in case anyone else is in a similar solution.

    In layouts/update_page.js.erb I put this file:

    $j('#<%=@update_div_id||"list_div"%>').html('<%= escape_javascript render(:partial => (@partial_div||"index"), :locals => @local_hash) %>');
    

    This is mainly used for searches that use remote, so in the index action in the controller, I just added the following code.

    respond_to do |format|
      format.html
      format.js {render 'layouts/update_page'}
    end
    

    Since remote is being used, it will always try to use javascript first, so it will render the update_page.js.erb file from above. For us, we almost always use the div#list_div on our index pages, so we update that by the default, however if you need to update something different, you can pass in @update_div_id, and if you need to render a different page, you can pass in @partial_div.

    To clarify, for a lot of things, it is probably better practice to use the callbacks, but I found this to be a much easier way, when we had to migrate over nearly 100 of these calls.

提交回复
热议问题