So I'm wondering what the best way to do static pages in Rails is, or rather Rails 3. I've always been a little confused about this, like should I create a controller or not?
If they're 100% static, just shove them into public.
For example, when you spin up a new rails project, there's an index.html
in your public.
If you want nicer routing, then yeah, creating a controller probably wouldn't be a bad idea.
It depends on if they're truly static. You can always add pages to the public/
directory of your app, and they'll work just fine. They don't even fire up rails or touch the routing engine.
However, most pages on the site, including static, still need to use the site's layout. You don't want to have to update the layout in dozens of pages separately. In this case, you can create a "catchall" controller. Here's an example:
rails g controller site home about_us location
Then you can put the page-specific content in app/views/site/home.html.erb
, for instance.
UPDATE: You can go a step further and cache those pages with a call to caches_page
near the top of your controller:
class SiteController < ApplicationController
caches_page :home, :about_us, :location
end
Just be aware that if you have dynamic page elements, like a list of links that changes based on whether the user is logged in, page caching isn't going to work for you. But this info should get you pointed in the right direction.
I prefer to create a controller. Good tutorial and explanation on static pages: http://railstutorial.org/chapters/static-pages#top