views:

95

answers:

2

I'm new to Ruby on Rails and I'm sure this question is pretty stupid, but I can't deduce the answer from the examples I've seen, perhaps I haven't seen many good ones.

A website I am working on will have 'boxes' (i.e. widgets) in one side bar that will contain dynamic content, and it will also have links on the other sidebar to sections of the website that will be handled by different controllers. The layout/style of the website will remain the same throughout the controllers. For example, if I click on one link, the content in the center will change but the rest of the site's design and layout will remain the same.

My question is if I should create something like a home controller which will handle the front page and the site's layout, and then somehow yield to a specific controller's layout should it be invoked, then specify that controller as the root controller in routes.rb? I am simply wondering how I would go about specifying that the layout should remain relatively the same, so that I don't have to be redundant and paste the same layout code for each controller. This is what someone on IRC recommended, to create a home controller, but I wanted to know if this was the normal way of doing things. The answer could actually be really simple for all I care, but since I'm new to rails, I'm oblivious.

I don't really know how to word my question properly since I'm relatively new to rails. If something does not make sense please let me know and I will try to clarify.

Thanks.

+2  A: 

Yes, you can use a page_controller for this and set this as the root url in routes.rb.

Keep in mind you don't need to use a separate layout file for each controller if you don't want to. If you create a file in layouts/application.html.erb, it will be used throughout the application. You can override it by creating other layouts that are named according to the controller which will get picked up automatically, or set layout :other_name in your controller to change it.

Andrew Vit
That's exactly what I wanted to know (Thanks to JRL too, upped you both). So then in the content div, which would change based on the controller in my app, how would I display the content based on the controller? Would I create a view for the controller and then render it as a partial within the content div? Let me know if I didn't make any sense heh.
Jorge Israel Peña
I just saw an example of application.html.erb and they used the yield directive. That answers my question, thanks!
Jorge Israel Peña
+1  A: 

If you create the file application.html.erb in your layouts folder it will be used by default by any controller that does not have a corresponding layout file in the layouts folder. Also, you can manually specify the layout to use in a controller by adding:

layout "your_layout_name"

in your controller.

JRL
Thanks JRL. If you can expand your answer to respond to my comment to Andrew, I would appreciate it.
Jorge Israel Peña
(Sorry for being redundant) I just saw an example of application.html.erb and they used the yield directive. That answers my question, thanks!
Jorge Israel Peña