Creating Static Pages w/ Rails ActionViews

by Calvin on May 24, 2009

Recently, I needed to create some static reporting pages for Skribit.  From a quick search, I got a lot of results that talk about Rails and static pages, but none did exactly what I needed:

  1. To be able to generate pages with different paths from one URL
  2. Pages to persist across Rails deployments

Not seeing any solutions that fit my needs, I set out to come up with my own.  Here is what I ended up with.

First, I needed to add a route for generating/displaying the reports:

From here, I could just use the standard caches_page :show declaration, but that would only generate the page I wanted if I used /report/2009/05/25 as the URL.  What if I wanted /report to generate the report for the current week?  Well, you can do something like this:

The magic is in the after_filter method cache_weekly_report.  We basically use the same mechanism Rails page caching uses to save our new report page.  Now, calling /report will generate a static report at /report/2009/05/25, or whatever the current day is.

The last thing to do is to make sure that the reports persist through new server deployments.  That can easily be done with a symlink in your capistrano script:

And that’s it!  What do you think?  I’d love to know if there are any simpler solutions to this.

Leave a Comment