Cowell Computer Consulting

Static Pages in Rails

I just read an interesting post on how to handle static pages in rails and I had a different approach that I wanted to share. There's lots of different ways to do this and each method has its own merit.

When I say static page, I mean any page that doesn't fit into your other controllers. This might be an about us or a contact page or anything else that might not require a dedicated controller to handle.

In routes.rb, I would set something like this up:

map.static '/static/:id', :controller => "static", :action => "show"

We can grab the name of the page from params[:id] in the controller. Feel free to change the name 'id' some something else if it gives you that wrong feeling to treat something as an id that isn't really an id.

Next, I define a controller called static.


class StaticController < ApplicationController
  def show
     short_name = params[:id]
     begin
       render short_name
     rescue ActionView::MissingPageTemplate
       render :file => "#{RAILS_ROOT}/public/404.html", :status => 404
     end
   end
end

So, what's happening here? We know we have an params[:id] or the route wouldn't match. We then call a begin block, so that we can rescue the exception. Which, in our case, is if someone passes an invalid filename through or the page hasn't yet been defined under app/views/static/pagename.html.erb. We just handle it as a 404.

What about if we want our static pages to live in a namespace without /static at the beginning ? ie. /about instead of /static/about

Define a regular expression that matches all the pages you want to support.

STATIC_PAGES = %w(about contact fun_facts)
STATIC_REGEXP = %r{#{STATIC_PAGES.join("|")}}

Then, in your routes file use the requirements parameter to match only these pages.

map.static ':id', :controller => "static", :action => "show", :requirements => {:id => STATIC_REGEXP}

This is just another approach and there are lots of ways to do this. I was inspired by Yehuda Katz to write this as he recently wrote a post about giving back to the community. Also, be sure to check out his posts about rails 3.

Posted by Luke Cowell on February 11, 2010 at 09:22 AM

Contributing to the Community

I'm proud to announce I made my first real contribution to the ruby / ROR community. I added a current quiz feature to the ruby quiz site and added an RSS feed to the site for all quizzes and solutions.

Announcement http://www.ruby-forum.com/topic/183984#809279

Ruby Quiz RSS Feed: http://rubyquiz.strd6.com/quizzes.rs

Ruby Quiz Site: http://rubyquiz.strd6.com/

Posted by Luke Cowell on April 23, 2009 at 11:13 PM

SVN Setup for Rails Apps

A lot of machine specific information doesn't need to go into the repository for your rails app. The following is taken from http://tuples.us/rails-guide/ which is largely taken from the ugly rails wiki.

svn remove log/*
svn commit -m 'removing all log files from subversion'
svn propset svn:ignore "*.log" log/
svn update log/
svn commit -m 'Ignoring all files in /log/ ending in .log'
svn remove tmp/*
svn commit -m 'removing all tmp files from subversion'
svn propset svn:ignore "*" tmp/
svn update tmp/
svn commit -m 'Ignoring all files in /tmp'
svn move config/database.yml config/database.yml.example
svn commit -m 'make database.yml template'
svn propset svn:ignore "database.yml" config/
svn update config/
svn commit -m 'ignoring database.yml'

It may also be a good idea to add:

svn remove db/*.sqlite3
svn propset svn:ignore "*.sqlite3" db/
svn update db
svn commit -m ' removing sqlite dbs from svn'

Posted by Luke Cowell on September 22, 2008 at 04:08 PM

Installing produCtion based mysql gem on OS X

The default mysql database adapter that ships with OS X is not optimized for produCtion use (read: not written in C).

Installing this gem on OS X can be a bit tricky because mysql isn't where the gem installer expects it to be. You attempt to install the gem and the compile fails because it can't find libraries etc. If you've installed mysql via the installer available on mysql's website (vs. ports) these instructions should work for you.

sudo gem install mysql -- --with-mysql-dir=/usr/local/mysql --with-mysql-include=/usr/local/mysql/include/ --with-mysql-lib=/usr/local/mysql/lib/ --with-mysql-config=/usr/local/mysql/bin/mysql_config

I was doing some heavy data crunching and switching to the production gem has definitely decreased cpu usage.

I should also credit this site: http://weblog.imapenguin.com/articles/2006/02/01/mac-os-x-10-4-ruby-mysql-adaptor

I'm not sure if this is where this originated.

Posted by Luke Cowell on June 28, 2008 at 07:01 AM

Blog Software

I'm using a little something called simplelog for my site. I decided to use it based on the following criteria:

  • Built on Ruby on Rails

    A Web development platform I'm very interested in.

  • Simple to use
  • Good built in theme

    I have no design ability.

  • Can link directly to posts

    Many of my posts will be for the benefit of my clients and peers and I need to be able to link directly

  • Tags

    I will be posting on a wide variety of subject, that in combination with my bad memory means that I might be making the same posts twice. The same posts twice.

I'm sure that there's other great blog tools out there. What are you using ? What are you staying away from ?

Posted by Luke Cowell on April 10, 2007 at 08:00 AM