0

I am building a rails app that has a number of models, one of which is Items.

Items contains a short list of products and supporting information.

I need to display this information on every page in the app.

In my views I've added

<%= render @items %>

and then added the partial 'items/_item.html.erb'

Now, to get this all to work, I also need to define the variable

@items = Item.all

My question is, what is the correct way to define this variable? I could add this line to every view for every controller, but that doesn't seem very DRY.

Should I be defining this in the application controller? If so, will this cause any issues if I also want to maintain access to the Items index page?

Sorry if this is a simple question. I'm trying to think through the best approach, and haven't found much written about this case. Grateful for fresh ideas and perspective!

Thank you!

Andy Harvey
  • 12,333
  • 17
  • 93
  • 185

3 Answers3

3

Do definitely should not call database from the view.

You can create private method inside application controller, and use it as before_filter for all controllers where @items collection is required. Most probably, you should customize actions where @items should be populated, like:

before_filter :load_items, :only => [:show, :edit, update]
taro
  • 5,772
  • 2
  • 30
  • 34
  • thanks @taro this is exactly what I was looking for. Thanks for showing me the way. I added `before_filter :load_items` to the applications controller, and below this `def load_items @items = Item.all end`. Works like a charm! – Andy Harvey Jan 15 '12 at 12:37
1

If you need to display something, you should consider doing it in the Application controller, instead of in all the different controllers, and the view parts you could do in the layouts (since that is common through out the app). You can have the partial in the same folder (i.e. layouts), and call it in the layouts view as

<%= render :partial => 'partial_name' %>

And since you've done this at the application level, I don't think you'll required to render this partial from anywhere else in your app.

Wahaj Ali
  • 4,093
  • 3
  • 23
  • 35
  • thanks wahaj, I also felt that this should be handled in the application controller, just couldn't figue out how to do it. Taro's suggestion got me think along the right lines – Andy Harvey Jan 15 '12 at 12:40
0

I think the easiest thing to do is to build a new partial (let's say we call it "items_box"). In your items/_items_box.html.erb, you define @items as Item.all, and supply whatever rendering code you need. Then all you have to do is change <%= render @items %> to <%= render 'items/items_box' %>.

Mark Tabler
  • 1,421
  • 10
  • 16
  • thanks mark, the problem I ran into was that I could not figure out how to define @items from within the partial. could you show me how you would do this? thanks for helping me learn – Andy Harvey Jan 15 '12 at 12:39
  • Well, if you were going to do it this way, it would just be a line that read `<%= @items = Item.all %>` at the top of your `_items_box.html.erb` file. But, I think Taro's approach is a cleaner one; he's right in that the View layer shouldn't handle database requests. :) – Mark Tabler Jan 15 '12 at 12:46