Update 2: This tutorial is way out of date. If you want to create a modern (ie. 2015) app, please use a generator like RailsBricks.
Update: Tutorial for Rails 3.1 here!
Every Rails developer knows how to create a Rails app. It’s easy as
rails new APP-NAME-HERE
But how many know how to create an app from scratch that looks good enough to sell to clients i.e. with slick design, authentication, authorization, and all that? Sure, there are tutorials out there that cover those components, but most of them cover them only in isolation from each other. A quick tutorial containing multiple components at a time would be a valuable resource to anyone planning to start a new app.
As the resident dilettante in these parts, I’ve decided to create such a tutorial based on a recent demo I made for a prospective client.
This post will discuss how to create a Rails 3 application that looks good enough to sell to clients (of course, YMMV) while still having components found in “professional” apps. This tutorial will cover the following:
- Andrea Franz’s web-app-theme gem – I only recently found out about this theme generator gem. Had I discovered this sooner, I might not have made “I hate web design” a catchphrase. Basically this gem generates themes for your web app (hence the name). The demo and the list of available themes can be found on this page. The main downside to this gem is the lack of tutorials on the net about it. Hopefully this post will give people an idea what to expect with the gem.
- Devise – our authentication module. Authlogic‘s fine, but I find Devise’s approach less obtrusive.
- Haml – replaces Erb. Not perfect (e.g. screws up with inline a tags) but the drastic decrease in code makes passing it up difficult.
- Rspec – replaces Test::Unit. Only setup will be covered in this post, actual usage is left to the reader.
- will_paginate – gem for pagination. Everybody uses it, so what the hell…
Rest of the tutorial is below the cut.
Let’s start by creating our new app with no testing framework and with MySQL as the database:
$ rails new pro-template-app -d mysql -T
Now would be a good time to edit the
.gitignore file to exclude files you don’t want to add to Git. Rails 3 already provides some good defaults, but if you want more possible entries there are samples available over at Github.
Once done, it’s time to make the initial commit.
$ git init
$ git add .
$ git commit -am "initial commit"
Now is time to add the needed gems to
source 'http://rubygems.org' gem 'rails', '3.0.3' gem 'mysql2' gem 'haml' gem 'will_paginate', '3.0.pre2' gem 'devise' group :development, :test do gem 'capybara' gem 'rspec-rails' gem 'haml-rails' gem 'hpricot' gem 'ruby_parser' gem 'web-app-theme' end
I’ve already explained most of the gems above at the beginning of this post. The only thing worth noting would be the odd version of
will_paginate (no “release” version is ready yet for Rails 3) and the addition of
ruby_parser, both of which are needed by both
web-app-theme to generate haml files.
bundle install” to install the gems. After the gems are installed, all of our generators will now use Rspec instead of Test::Unit, and it can now generate Haml instead of Erb.
Generate home page
Let’s try out our app first by creating a new root page to replace the default.
$ rails g controller home index
$ rm public/index.html
Then edit the
ProTemplateApp::Application.routes.draw do root :to => "home#index" end
Before starting the app, don’t forget to create the database:
$ rake db:create
Start the server.
Generate theme with web-app-theme
Here comes the fun part.
$ rails g web_app_theme:theme --theme="drastic-dark" --engine=haml --app-name="My Latest Web App"
This will generate the layout files using the “Drastic Dark” theme in haml whose title would be “My Latest Web App”.
Delete the default layout generated for the rails app:
$ rm app/views/layouts/application.html.erb
then refresh the page.
Jan 10 update: Andrea Franz also mentioned that you could use the option
--themed-type=text for this step to generate a text template which you could manipulate as needed.
Note that the file created would be
show.html.haml so unless you decided to treat the home controller as a resource (i.e. use
show instead of
index as the default page), you will have to rename/overwrite the said files.
$ rails g web_app_theme:themed home --themed-type=text --engine=haml
$ mv app/views/home/show.html.haml app/views/home/index.html.haml
Add Devise authentication
Let’s install Devise and create our User model in the process.
$ rails g devise:install
$ rails g devise user
We also need to customize the login screen so we’ll generate the Devise views.
rails g devise:views -e haml
Now we need to tweak the UI to use the web-app-theme. First, let’s generate the sign in layout:
$ rails g web_app_theme:theme sign --layout-type=sign --theme="drastic-dark" --engine=haml --app-name="My Latest Web App"
Then let’s add the following files to the
config/application.rb file to change the layout for signing in:
config.to_prepare do Devise::SessionsController.layout "sign" end
After that comes the tedious part of having to modify the Devise
app/views/devise/sessions/new.html.haml file to match the one expected by web-app-theme. Fortunately for you, you could just copy-paste what I did here.
Restart the server, run
rake db:migrate, and try going to the login site (
Finishing touches to Devise
Let’s do some finishing touches to the UI.
Now to test if Devise really works. Add the
before_filter :authenticate_user! to
home_controller.rb to kick us out to the login screen if we’re not logged in.
Create a test user via the rails console.
$ rails c
User.create :email => "email@example.com", :password => "123456", :password_confirmation => "123456"
You should now be able to login and logout.
Theming a CRUD module
Now let’s show how to add the web-app-theme to a new CRUD module. To simplify things, we’ll use the ultra-
stupidsimple blog demo.
$ rails g scaffold blog_entry subject:string content:text publish_at:datetime
$ rake db:migrate
Overwrite the generated files by running web-app-theme’s generator:
$ rails g web_app_theme:themed blog_entries --will-paginate --engine=haml
And after that…
The rest is up to the reader. Here are some gems that you might want to look at for your application, though:
- Cancan – clients usually have roles in their apps. If you have more than 3 roles, I suggest you use this gem to manage user rights.
- Delocalize – add this gem unless you want to manually handle your clients entering 10,000.00, saving it as 10000.00 in the database instead of 10. Also works on dates.
- Spreadsheet – web apps usually have a Batch Job and Reports component. While I’ve gone by with using the “runner + cron” for batch jobs, clients don’t really find CSV reports appealing. This gem allows you to generate Excel reports for them.
- Capistrano – for deployment. This gem warrants its own tutorial.
And that’s it. The complete code generated above can be found at Github.
Follow up: Steps to style the sign up page here.
Follow up 2: As pointed out by Andrew below, Devise has removed HAML support. Now you must generate views as erb via
$ rails g devise:views -e erb
html2haml manually on the records e.g.
$ html2haml app/views/devise/sessions/new.html.erb app/views/devise/sessions/new.html.haml