Update HTML Element with Ajax

I have following files.

list.rhtml

    <%= render(:partial => 'item', :collection => @items) %>

_item.rhtml

  • <%= item.name %> <%= item.body %>
  • edit.rjs

    page.replace_html @item.id, :partial=>‘item’

    if I call /items/list I will get

    • ...
    • ...
    • ...

    And wenn I edit first item I will get

    • ...
    • ...
    • ...

    Sure I can controll it in _item.rhtml partial like this
    _item.rhtml

    <% if some_merker %>

  • <% end %> <%= item.name %> <%= item.body %> <% if some_merker %>
  • <% end %>

    and then set this merker=true for list-action and false for others, but
    it’s unelegant.

    Some Ideas?

    Sorry for my english.

    I generated a Patch for Rails to add the ability to do what you want, as
    I
    had the same issue - http://dev.rubyonrails.org/ticket/3246

    To work around this with the existing code, and without applying the
    patch,
    you can change your edit.rjs to this (copied from the patch code,
    written in
    email, so may need a little work):

    partial = render :partial => ‘item’
    page << “var html = ‘#{partial}’;”
    page << “element = $(#{item.id});”
    page << “if (element.outerHTML) {”
    page << " element.outerHTML = html.stripScripts();"
    page << “} else {”
    page << " var range = element.ownerDocument.createRange();"
    page << " range.selectNodeContents(element);"
    page << " element.parentNode.replaceChild("
    page << " range.createContextualFragment(html.stripScripts()),
    element);"
    page << “}”