views:

835

answers:

2

Hi All,

I'm rendering a partial in a collection like this :

<%= render :partial => 'issues/issue', :collection => @issues %>

Inside the partial, I want to render a


element unless it's the last in the collection. I could of course, render the partial like this

<%= render :partial => 'issues/issue', :collection => @issues, :locals => {:issue_count => @issues.length } %>

then put this inside my partial

<% unless issue_counter + 1 == issue_count %>
  <hr />
<% end %>

but I don't want to have to explicitly set the local in the render call, and I the collection isn't always going to be called @issues, so I can't just access the instance varibale. Is there some way to access the length of the collection automatically inside the partial to tell where in the collection the object falls? If there's not already, is it possible to add this in such a way that I'll automatically get the issue_count local? Any help on this will be much appreciated.

Thx,

-C

A: 

I think you can make this happen if you modify the render_partial_collection method.

As a side note: Seems to me like you should instead use CSS on a unordered list: I get the feeling you are inserting markup HRs to style instead of using it to semantically separate items in the collection (in mark-up semantics).

Felix Ogg
yeah, maybe so, but it's fast. also, it's a good thing to know how to do :)
Chris Drappier
It's no faster than just wrapping the content of your partial in a <li></li> tag set instead.
Brendon Muir
+3  A: 

You can supply the :spacer_template option to your render :partial => X, :collection => Y call. See the ActionController::Base documenation for usage.

It probably feels heavy-handed to specify an entire partial file for a simple <hr /> element, but going this route keeps your intention clear and keeps the item partial free of unrelated divider markup.

Duncan Beevers
while this doesn't really get me the count inside the partial, it does seem to eliminate the need for it :)thx-C
Chris Drappier