Things I commonly do when starting a Rails project

Here’s another post from me for me. When starting a new Ruby on Rails project, I tend to do several things the same each time.

1. Setup SVN repository. Most of my projects are completely unrelated, so I usually have to setup a separate repository. The best walkthrough I’ve found is here.

2. Create development/test databases in mysql:

create database project_development;
create database project_test;

3. Setup ActiveRecord sessions:

rake db:sessions:create
rake db:migrate

(uncomment line in config/environment.rb)

config.action_controller.session_store = :active_record_store

4. Setup initial controller…something like:

script/generate controller Main index

And make it the default route (edit the file config/routes.rb)

# Install the default route as the lowest priority.
map.connect ':controller/:action/:id', :controller => 'main'

(Don’t forget to delete or rename public/index.html)

5. I almost always need user accounts. The ActsAsAuthenticated plugin works well:

script/plugin install
script/generate authenticated user account
rake db:migrate

Look in the newly created app/controller/account_controller.rb for a few things to move to app/controller/application.rb

6. While editing app/controller/application.rb, here are a bunch of date/time formatting shortcuts I use a lot:
:date_time12 => "%m/%d/%Y %I:%M%p",
:regular => "%b %d %I:%M %p",
:date_time24 => "%m/%d/%Y %H:%M",
:pretty_date => "%B %d, %Y",
:month_year => "%B %Y",
:short_month_year => "%b %Y",
:month_day => "%b %d",
:pretty_date_time => "%B %d, %Y %I:%M %p",
:short_date => "%b %d, %Y",
:short_12 => "%b %d %I:%M %p",
:standard => "%m/%d/%Y"
:pretty_date => "%B %d, %Y",
:month_year => "%B %Y",
:short_month_year => "%b %Y",
:year_only => "%Y",
:month_day => "%b %d",
:short_date => "%b %d, %Y",
:standard => "%m/%d/%Y"

Just add all of that to the bottom of application.rb, then use like this: object.created_at.to_s(:standard)

7. Other essential plugins include:


script/plugin install


script/plugin install
mv vendor/plugins/trunk vendor/plugins/filecolumn


script/plugin install


script/plugin install
mv vendor/plugins/trunk vendor/plugins/dynamic_session_exp

8. Create a default layout for all controllers: app/views/layouts/application.rhtml (If you have a controller needing its own layout, just create one called, “controller.rhtml”) Here is a basic application.rhtml:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">
<html xmlns="">
<title><%= @page_title || 'Site Name' %></title>
<%= stylesheet_link_tag 'all', :media=>'all' %>
<%= stylesheet_link_tag 'print', :media=>'print' %>
<%= javascript_include_tag :defaults %>
<%= calendar_date_select_includes %>
<!--[if lt IE 7]>
<script defer src="/javascripts/pngfix.js" type="text/javascript"></script>
<% if flash[:notice] -%>
<div class="flash_notice" id="Flash"><%= flash[:notice] %></div>
<% end -%>
<% if flash[:alert] -%>
<div class="flash_alert" id="Flash"><%= flash[:alert] %></div>
<% end -%>
<%= yield %>

9. Default public stuff (pngfix.js, latest scriptaculous, stylesheets, images, etc.)


One Response

  1. eh?

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: