Where to place a query that should be included in all or most views

Posted by Andrew on Stack Overflow See other posts from Stack Overflow or by Andrew
Published on 2011-11-26T17:47:36Z Indexed on 2011/11/26 17:51 UTC
Read the original article Hit count: 246

In my application I have a sidebar which I want to include a list of pages. Cheating on the MVC setup, I can pretty easily display this as follows (in HAML):

# layouts/_sidebar.html.haml
%h4 Pages
%ul.pages
  - for page in Page.all
    %li= link_to page.title, page

Now, this works just fine, but clearly it's against the convention. The problem is, this shared layout partial is present in most (but not all) views, and therefore to serve the pages from the controller layer would mean needing to inject an instance variable into almost every controller action in the application. That isn't very clean or DRY.

So, how would you handle this kind of situation? Is there a clean, DRY place to put this kind of a simple query that respects Rails MVC convention better?

© Stack Overflow or respective owner

Related posts about ruby-on-rails

Related posts about mvc