A Tutorial

Not a whole lot of information exists right now about Rails 3. Basically since I am building an application from scratch, I will run it with Ruby 1.9.1, and rails 3. If you are coming from rails 2.3.x than a few things have changed. I wanted to post a quick tutorial on how to get started.

First setup your project

If you are running rvm (as I am make sure that you have loaded rails 3) see this post for more info. Since you have rails 3 loaded, which you can test with - rails -v and it should read something like - Rails 3.0.0.beta, you can next run rails fish which will create the rails3 directory structure. Next step into the directory cd fish.

Second Add in HAML

Rails 3 allows for many plugins but I have only test Haml to date. Instructions exist for this here. One of the new features of Rails 3 is the new Gemfile.

  1. In this file you will need to add gem “haml”.
  2. next run - bundle install - and it will load all the gems for the application
  3. Third run - haml —rails . to install the plugin.
  4. Fourth under config/application.rb add the following code:
    config.generators do |g|
    g.template_engine :haml
    end
  5. Next the git clone:
    git clone git://github.com/psynix/rails3_haml_scaffold_generator.git lib/generators/haml
  6. Finally we need to start running some applications.

Building the Application

Now we have the basic’s setup its time to see if it runs -
rails server should start the server. Just to make sure that it all works correctly. Next we need to build in some scaffolding - Since we are building an application called fish let’s make some types of fish. rails generate scaffold Minnow name:string size:integer color:string for simplicity we only setup a few fields. After that is completed simply run rake db:migrate and it should create the database and fields as are needed. again run rails server to start the application navigate to: http://localhost:3000/minnows and you can add, show and remove all the little fish that you like.

Generator

Last is to note the Generator - rails generate to know which commands you can do from the command line:

Rails:
controller | generator | helper | integration_test | model_subclass | observer | performance_test | plugin | resource | scaffold | scaffold_controller session_migration | stylesheets |

ActiveRecord: active_record:migration | active_record:model | active_record:observer | active_record:session_migration |

Erb: erb:controller | erb:mailer | erb:scaffold |

Haml: haml:controller | haml:mailer | haml:scaffold |

TestUnit: test_unit:controller | test_unit:helper | test_unit:integration | test_unit:mailer | test_unit:model | test_unit:observer | test_unit:performance | test_unit:plugin | test_unit:scaffold |

I really am liking Rails 3 - Just to have the modular architecture is quite nice.

Published on in rails3