I''m having an issue with the design of my views that I can''t
seem to
wrap my head around.  I have a page where I am showing a collection of
objects in a list.  The way that I designed it, it looked something
like this:
main view:
<h2>here is the list</h2>
<ul>
    <%= render(:partial => "items/item", :collection =>
@items %>
</ul>
collection item''s view:
 <li id="<%= dom_id(item)  %>">
     ...  INFO ABOUT item....
</li>
This works great, creating a <ul> that is full of <li>''s. 
I hit a
snag when I start trying to update individual <li> items with ajax
calls.  The :update option for  link_to_remote() and related functions
doesn''t give me an option to replace the dom element that it refers to
as far as I can tell.  It lets me specify a dom id that I can change
the innerHTML of, insert the returned html with in it at the beginning
or the end, before or after the element, but it doesn''t seem to allow
me to REPLACE the element.
So a call like this:
remote_function(:url => credential_search_instance_path
(credential_search_instance),
                             :method => :put,
                             :update => dom_id(item)
                           )
Will replace the html inside of the <li> element, so it would update
the page to look like this:
 <li id="item_1">
 <li id="item_1">
     ...  INFO ABOUT item....
</li>
</li>
I ended up getting around the issue by checking if it is an xhr
request in my item view like this;
<% if ! request.xhr? then %>
 <li id="<%= dom_id(item)  %>">
<% end %>
     ...  INFO ABOUT item....
<% if ! request.xhr? then %>
</li>
<% end %>
That solution works, but it seems very inelegant and adds a lot of
noise to the view.  Is it really true that I can''t replace the dom
element?  Am I missing something?  Is there a better way to design my
view and partial?
Thanks,
Jonathan
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---
Mark Reginald James
2009-Jan-02  11:07 UTC
Re: link_to_remote can''t replace specified element?
JHuizingh wrote:> I''m having an issue with the design of my views that I can''t seem to > wrap my head around. I have a page where I am showing a collection of > objects in a list. The way that I designed it, it looked something > like this: > > main view: > > <h2>here is the list</h2> > <ul> > <%= render(:partial => "items/item", :collection => @items %> > </ul> > > > collection item''s view: > > <li id="<%= dom_id(item) %>"> > ... INFO ABOUT item.... > </li> > > > This works great, creating a <ul> that is full of <li>''s. I hit a > snag when I start trying to update individual <li> items with ajax > calls. The :update option for link_to_remote() and related functions > doesn''t give me an option to replace the dom element that it refers to > as far as I can tell. It lets me specify a dom id that I can change > the innerHTML of, insert the returned html with in it at the beginning > or the end, before or after the element, but it doesn''t seem to allow > me to REPLACE the element. > > So a call like this: > > remote_function(:url => credential_search_instance_path > (credential_search_instance), > :method => :put, > :update => dom_id(item) > ) > > Will replace the html inside of the <li> element, so it would update > the page to look like this: > > <li id="item_1"> > <li id="item_1"> > ... INFO ABOUT item.... > </li> > </li> > > > I ended up getting around the issue by checking if it is an xhr > request in my item view like this; > <% if ! request.xhr? then %> > <li id="<%= dom_id(item) %>"> > <% end %> > ... INFO ABOUT item.... > <% if ! request.xhr? then %> > </li> > <% end %> > > That solution works, but it seems very inelegant and adds a lot of > noise to the view. Is it really true that I can''t replace the dom > element? Am I missing something? Is there a better way to design myThe best solution would be to move the li tags to a layout: render :partial => "items/item", :collection => @items, :layout => ''li'' Where _li.html.erb contains <li><%= yield %></li> However this is broken in 2.2.2 (it instead renders a collection of the collection). I''ve created a ticket: http://rails.lighthouseapp.com/projects/8994-ruby-on-rails/tickets/1688 So until this works, the other options are: 1. Replace the collection with a loop, either using the layout option, or putting the li tags in the main view, 2. Create a second level of partial, or 3. Remove the :update option and use outer-HTML replace RJS calls. -- Rails Wheels - Find Plugins, List & Sell Plugins - http://railswheels.com --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---