My application has some static content:
root :to => 'pages#home'
match '/about', :to => 'pages#about'
match '/contact', :to => 'pages#contact'
match '/help', :to => 'pages#help'
I want these pages to share a layout rather than duplicate the header / footer in each file.
The only documentation around layouts I have found seems to indicate that I should specify my layouts at the controller level, is that correct?
If so, how should give my static pages layouts? The only workaround I see is to create a bunch of empty controllers for each page, for the sole purpose of specifying a common layout file, but that smells like overkill.
Am I missing a trick?
Thanks for any help
Yes, set the layout at the controller level. You could create a controller for this group of pages like this:
I tend to use the name “AboutController” for this sort of thing, to avoid confusion with truly static files that don’t pass through a controller at all. But you could name it whatever you like.
Point your routes at it (e.g.
about#contact). Then create the layout inapp/views/layouts/about.html.erbIf you ever need to change the layout for a particular action, you can use the
rendermethod’s:layoutoption:You can also pass false if you don’t want a layout at all, like for robots.txt.