Forum: Ruby on Rails Update HTML Element with Ajax

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
0ff7e5d45c4da4a1719efd9c8caaa404?d=identicon&s=25 Roman Ilin (purplehaze)
on 2006-01-20 17:27
I have following files.

list.rhtml
-----------------------
<ul id="items">
  <%= render(:partial => 'item', :collection => @items) %>
</ul>

_item.rhtml
------------------------
<li id="<%= item.id %>" >
  <%= item.name %>
  <%= item.body %>
</li>

edit.rjs
------------------------
page.replace_html @item.id, :partial=>'item'


if I call /items/list I will get

<ul id="items">
  <li id="item_1">...</li>
  <li id="item_2">...</li>
  ...
</ul>

And wenn I edit first item I will get
<ul id="items">
  <li id="item_1"><li id="item_1">...</li></li>
  <li id="item_2">...</li>
  ...
</ul>

Sure I can controll it in _item.rhtml partial like this
_item.rhtml
------------------------
<% if some_merker %>
<li id="<%= item.id %>" >
<% end %>
  <%= item.name %>
  <%= item.body %>
<% if some_merker %>
</li>
<% end %>

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

Some Ideas?

Sorry for my english.
3f900b38ec3b2c45427c354722fa4ce3?d=identicon&s=25 Tom Fakes (tomfakes)
on 2006-01-20 20:12
(Received via mailing list)
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 << "}"
This topic is locked and can not be replied to.