views:

152

answers:

1

I think I'm approaching this the wrong way, which is why I can't seem to figure out an easy solution. I'm relatively new to Rails, so bear with me.

I have a layout, "store," that contains all of the common visual elements for the other controllers. In this layout, I need to dynamically create the sidebar with data from two models, Product and ProductFamily. I want it to output like so:

Product Family
---- Product1
---- Product2

And continue on through all relevant records.

I just can't figure out where to start writing the logic for this. Does it go in the layout? In a controller?

Please, point me in the right direction!

+2  A: 

It's not "logic" in the sense we use it when saying that you should keep logic out of the views. It's just a simple iteration for the purposes of display, so it would go in the view. You'd pass in the product line and do something along the lines of:

<% @product_line.each do |product| %>
<%= product.name %>
<% end %>

Add all the styling and links you want.

Pesto
So, a "layout" can be treated the same way a view is treated? I know how to iterate through records on a view, but those views are tied to a specific controller. In my case, it's a common element that needs to be generated from the database and shared on all pages--which is why I opted to put it in a "layout" template instead, and not a view. Is it still the same?
I'm a doofus, forget the above comment. I accidentally commented out a parenthesis while experimenting which made it throw an error, thus confusing me. Thanks for the help!