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

前端 未结 3 1446
无人及你
无人及你 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:06

    The most straightforward way to do this would be to write a javascript view template, e.g. create.js.erb which would look something like this:

    $('#div_id').html("<%= escape_javascript(render(@item)) %>");
    

    (depending on your setup, of course, I'm assuming an @item variable and an associated _item partial)

    Edit:
    coreyward is right. This is the RJS way which is more of the old fashioned Rails 2.x "Rails way". It's probably more familiar, but has issues. Your specific case is one of them, actually, as typically you might bind to an HTML element to update using the record's id (e.g. div #item_1), and in the create case there is no id available beforehand, complicating matters.

    Binding via clientside JS eliminates this issue. RJS works in something of a vacuum, making assumptions about the state of the client's HTML and having no access to it.

提交回复
热议问题