I wanted to get the community opinion on the best way to use partials and inline list (<li>) editing. I have a very common scenario where a list is rendered: --- index.rhtml --- <ul> <%= render :partial => ''item'', @collection => @items %> </ul> --- _item.rhtml (simplified) --- <li id="item_<%= item.id %>"> <div id="show_item_<%= item.id %>"> <%= h item.name %> <%= link_to ''edit'', . %> </div> <div id="edit_item_<%= item.id %>" style="display: none"></div> </li> I am using RJS templates to populate the ''edit_item'' div on an ajax call from the ''edit'' link. All very straightforward stuff. The problem is on update in my RJS template I want to render the partial again to update the list item like this: --- update.rjs --- page.replace_html "item_" + item.id.to_s, :partial => ''item'' The problem of course is that then you have nested <li> elements as the inner text is replaced, not the entire tag. Has anyone else encountered this and come up with an elegant solution. I suppose I could strip the outer <li> elements from the partial only in the RJS call like this: page.replace_html "item_" + item.id.to_s, my_method_that_strips_the_outer_li( render :partial => ''item'') But I have a feeling that there is a much more elegant way to do this. Ideas? Thanks, Zack
Zack, Support was just added for ''replace''. Take a look at http://dev.rubyonrails.org/changeset/3590 and http://dev.rubyonrails.org/changeset/3579, which give more detail. I haven''t actually tried it yet, but it may solve your problem. Don''t forget to do rake update_javascripts. On 2/14/06, Zack Chandler <zackchandler@drainpatrol.com> wrote:> I wanted to get the community opinion on the best way to use partials and > inline list (<li>) editing. > I have a very common scenario where a list is rendered: > > --- index.rhtml --- > <ul> > <%= render :partial => ''item'', @collection => @items %> > </ul> > > --- _item.rhtml (simplified) --- > <li id="item_<%= item.id %>"> > <div id="show_item_<%= item.id %>"> > <%= h item.name %> > <%= link_to ''edit'', . %> > </div> > <div id="edit_item_<%= item.id %>" style="display: none"></div> > </li> > > I am using RJS templates to populate the ''edit_item'' div on an ajax call > from the ''edit'' link. All very straightforward stuff. The problem is on > update in my RJS template I want to render the partial again to update the > list item like this: > > --- update.rjs --- > page.replace_html "item_" + item.id.to_s, :partial => ''item'' > > The problem of course is that then you have nested <li> elements as the > inner text is replaced, not the entire tag. Has anyone else encountered > this and come up with an elegant solution. I suppose I could strip the > outer <li> elements from the partial only in the RJS call like this: > > page.replace_html "item_" + item.id.to_s, > my_method_that_strips_the_outer_li( render :partial => ''item'') > > But I have a feeling that there is a much more elegant way to do this. > Ideas? > > Thanks, > Zack > > > > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-- Cody Fauser http://www.codyfauser.com
Thanks Cody! That looks like just what I was looking for! Zack -----Original Message----- From: rails-bounces@lists.rubyonrails.org [mailto:rails-bounces@lists.rubyonrails.org] On Behalf Of Cody Fauser Sent: Tuesday, February 14, 2006 11:14 AM To: rails@lists.rubyonrails.org Subject: Re: [Rails] Inline list editing with RJS: best practices Zack, Support was just added for ''replace''. Take a look at http://dev.rubyonrails.org/changeset/3590 and http://dev.rubyonrails.org/changeset/3579, which give more detail. I haven''t actually tried it yet, but it may solve your problem. Don''t forget to do rake update_javascripts. On 2/14/06, Zack Chandler <zackchandler@drainpatrol.com> wrote:> I wanted to get the community opinion on the best way to use partials and > inline list (<li>) editing. > I have a very common scenario where a list is rendered: > > --- index.rhtml --- > <ul> > <%= render :partial => ''item'', @collection => @items %> > </ul> > > --- _item.rhtml (simplified) --- > <li id="item_<%= item.id %>"> > <div id="show_item_<%= item.id %>"> > <%= h item.name %> > <%= link_to ''edit'', . %> > </div> > <div id="edit_item_<%= item.id %>" style="display: none"></div> > </li> > > I am using RJS templates to populate the ''edit_item'' div on an ajax call > from the ''edit'' link. All very straightforward stuff. The problem is on > update in my RJS template I want to render the partial again to update the > list item like this: > > --- update.rjs --- > page.replace_html "item_" + item.id.to_s, :partial => ''item'' > > The problem of course is that then you have nested <li> elements as the > inner text is replaced, not the entire tag. Has anyone else encountered > this and come up with an elegant solution. I suppose I could strip the > outer <li> elements from the partial only in the RJS call like this: > > page.replace_html "item_" + item.id.to_s, > my_method_that_strips_the_outer_li( render :partial => ''item'') > > But I have a feeling that there is a much more elegant way to do this. > Ideas? > > Thanks, > Zack > > > > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-- Cody Fauser http://www.codyfauser.com _______________________________________________ Rails mailing list Rails@lists.rubyonrails.org http://lists.rubyonrails.org/mailman/listinfo/rails