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
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”
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.
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.
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
Post a Comment