Issue
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>
Solution
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
Answered By - DGM Answer Checked By - Cary Denson (PHPFixing Admin)
0 Comments:
Post a Comment
Note: Only a member of this blog may post a comment.