Rendering a partial twice, with different objects

I have a form in which I want to render a list of full beers and empty
beers. I have a partial that displays the details of the empty or full
beers. I would like to use the same partial twice in one form to
display details of full and empty beers. Here's what I'm doing:

beers_controller.rb
  def new
    @beers = Beer.find(:all)
  end
end

beers/_beer_list.html.erb
<% @beers.each do |beer| %>
  <%= beer.name %> is <%= beer.status %>
<% end %>

I want to do something like this in the view beers/new.html.erb:
...
<%= render :partial => 'beer_list', :locals => { :beers =>
@beers.select { |a| a.status == 'full' } %>
...
<%= render :partial => 'beer_list', :locals => { :beers =>
@beers.select { |a| a.status == 'empty' } %>
...

so that I can separately list the full and empty beers from one
instance variable using the same partial. This approach gives me the
same list for full and empty beers right now... This seems the DRYest
approach, what am I doing wrong? Thanks for any advice.

SH

I have a form in which I want to render a list of full beers and empty
beers. I have a partial that displays the details of the empty or full
beers. I would like to use the same partial twice in one form to
display details of full and empty beers. Here's what I'm doing:

beers_controller.rb
def new
  @beers = Beer.find(:all)
end
end

beers/_beer_list.html.erb
<% @beers.each do |beer| %>
<%= beer.name %> is <%= beer.status %>
<% end %>

On top of the advice Ryan has given you, the key thing here is that in
your partial you should not iterate over @beers: that instance variabl
is invariant so you're not going to get anywhere.

In you partial you should use the object you've passed though, so
(assuming you've renamed the partial as ryan suggests) the partial
should just be
<%= beer.name %> is <%= beer.status %>

And your call to render should be
<%= render :partial => 'beer', :collection => @beers.select { |a|
a.status == 'full' } %>
which you can shorten to
<%= render :partial => @beers.select { |a| a.status == 'full' } %>

If you tell rails to render a collection of Beer objects, rails will
guess that you want to use the beer partial. With either of these
approaches you don't need to iterate inside your partial: render does
it for you.

Fred

Thanks Ryan and Frederick, works like a peach.

One weird thing is when using the syntax <%= render :partial =>
@beers.select ... %> I get the error:

Missing template arrays/_array.html.erb

But with the syntax <%= render :partial => 'beer', :collection =>
@beers.select ... %> no error. I'm on Rails 2.1.0 ... Am I missing
something?

Thanks again!

SH

shenry wrote:

Thanks Ryan and Frederick, works like a peach.

One weird thing is when using the syntax <%= render :partial =>
@beers.select ... %> I get the error:

Missing template arrays/_array.html.erb

Yeah, it looks like your trying to render "@beers.select..." instead of
a partial titled "beer". Check to make sure your code is similar to the
code suggested to you...(render :partial => 'beer', :collection =>
@beers.select)

Lake