0

I have a piece of code which displays a complicated 'listing' of resources. by complicated i mean that it has name, date, time posted, number of comments, picture, description, and tons more things that you obviously only want to write that block of code one time.

I'm trying to think of how this would go into the rails architecture and I'm at a loss

i need to display the resource listing using a different variable in different controllers. currently its in a partial called _resource_view.html.erb

This is fine for the main listing, but its not usable for the persons profile listing, which takes the same format but shows different resources

_resources_expanded.html.erb

<ul class="res-list">
  <% @resources.each do |resource| %>
    ... list stuff
  <% end %>
</ul>

It uses @resources on index, but on the profile page it uses the same listing code except its @user.resources.each do |resource|

I was thinking maybe something like this but this seems redundant .. and im not even sure if it will work

<ul class="res-list">
  <% @resources.each do |resource| %>
    <%= render 'layouts/resources_expanded' %>
  <% end %>
</ul>
Tallboy
  • 12,847
  • 13
  • 82
  • 173

1 Answers1

2

Don't use instance variables, use local variables in the partial - that lets you pass in a single each time through the @resources.each loop.

<%= render 'layouts/resources_expanded', :resource => resource %>

and then in _resource_view.html.erb change @resource to resource

DGM
  • 26,629
  • 7
  • 58
  • 79
  • 1
    I love you. I knew there had to be a way to do that and i forgot to put that in my original post. im glad my intuition told me thats probably how it was going to go, im passing complete noob level :) – Tallboy Apr 13 '12 at 01:46