views:

61

answers:

1

Hey guys. I'm creating a rails app and being one of the first times I've used the MVC pattern, let alone rails, I'm running into ambiguity and uncertainty. I already defined an application.html.erb which is working great, it has a yield statement which renders the current action's view.

I want application.html.erb to contain some dynamic content though, for example in a sidebar. I am wondering where the code for this dynamic content should go. While experimenting, I created a controller called home and defined the logic there, though I don't think it should go there. It's just that I didn't create a model because I don't need any form of database for this, and most of the examples I've seen use models specifically to interact with some form of persistent data. In the home controller I defined the index action, and when I go to mysite.com/home/ it shows the dynamic content I wanted to see, but in the content section of the layout (where the yield statement is).

Basically I feel all tangled up now, and I am wondering how I would go about placing this dynamic content in the side bar of my application.html.erb layout so that it remains throughout my site, since my entire site uses this layout. I was thinking about using application_controller.rb, but something tells me I should leave that alone.

Thanks, let me know if anything doesn't make sense. Pretty difficult to be clear when I am really confused.

+2  A: 

application_controller.rb is a fine place for your sidebar content to go. If your content is truly application wide, like sidebar navigation, for instance, then it's an appropriate place for it.

I recommend putting your html for your sidebar in a partial or two, and the logic that populates it in an application_controller action that will run for every page request, something like:

before_filter :populate_sidebar

def :populate_sidebar
    @sidebar_content = foo
end
Doug R
I appreciate the quick and accurate response Doug. I got the same response on IRC, so I take it it is the correct way of doing this.
Jorge Israel Peña