Skip to content

Rails Tip: Static Pages with No Extra Controller Methods

I was reading [this tip][1] about using method\_missing to catch requests for pages you haven’t created a method for in your Rails controllers, when I remembered something I’d read _somewhere_ a while back.

You don’t even need that tip. If you have some static page you want to show in your Rails app, just drop it in the appropriate place in `app/views/` - you don’t even need to put a stub method in the controller. That page will automatically show up, using any layout you might have specified and caching if you choose!

Cool, huh?

[1]: http://pjhyett.com/articles/2005/11/17/lets-talk-about-method_missing

4 Comments

  1. Are you sure it go0es in app/views? For example, if I want to create a static page at the URL /foo/bar.html then I put bar.html in app/views/foo? I’m trying this andf it does not seem to be working. I just get Routing Error

    Recognition failed for “/foo/bar.html”

    Posted on 30-Nov-05 at 12:58 pm | Permalink
  2. On further investigation this works when I put the pages in the public directory. It does not work when I put the static pages in app/views.

    Posted on 30-Nov-05 at 2:11 pm | Permalink
  3. Jake wrote:

    Well, yeah.. There’s the rub…

    If you place it within app/views, you’d still need a Foo controller, even if there’s no appropriate method for the page in the controller code.

    The main benefit of this is that these pages can inherit layout from the controller and use erb. If you don’t need either of those facilities, putting the file in public/foo makes more sense.

    Posted on 30-Nov-05 at 11:52 pm | Permalink
  4. Evgeny wrote:

    Actually it is also discussed elsewhere, and actually works ….

    Here by Josh:
    http://blog.hasmanythrough.com/2008/4/2/simple-pages

    And here by Markus:
    http://snafu.diarrhea.ch/blog/article/4-serving-static-content-with-rails

    Posted on 16-Apr-08 at 9:54 am | Permalink

Post a Comment

Your email is never published nor shared. Required fields are marked *
*
*