With Rails3 i can create a project fully cut to my needs. I will write it down here, just so i remember it well and hopefully it will help some of you too.
In my projects, i want to use haml, rspec2, factory-girl, jquery, …
We need a few steps to complete this.
Create the project
For starters, create the folder, without Test::Unit (-T) and without prototype (-J).
rails new test-project -T -J
You could also specify the database, using option
-d, [--database=DATABASE], with the following options:
- sqlite3 (default)
Specify needed gems
We also have to specify the gems we need. To do that, we need to edit the
Gemfile which is located in the root of your project. My file looks as follows:
source 'http://rubygems.org' gem 'rails', '3.0.0.rc' # Bundle edge Rails instead: # gem 'rails', :git => 'git://github.com/rails/rails.git' #gem 'pg' gem 'sqlite3-ruby', :require => 'sqlite3' # Use unicorn as the web server # gem 'unicorn' # Deploy with Capistrano # gem 'capistrano' # To use debugger # gem 'ruby-debug' gem 'rails3-generators' gem "bson_ext" gem "haml" gem "haml-rails" gem "jquery-rails" gem "rcov" # we need this here, see http://blog.davidchelimsky.net/2010/07/11/rspec-rails-2-generators-and-rake-tasks/ group :development, :test do gem "rspec-rails", ">= 2.0.0.beta.18" end # test-environment gems group :test, :spec, :cucumber do gem "factory_girl_rails" gem "rspec", ">= 2.0.0.beta.18" gem "remarkable", ">=4.0.0.alpha2" gem "remarkable_activemodel", ">=4.0.0.alpha2" gem "remarkable_activerecord", ">=4.0.0.alpha2" gem "capybara" gem "cucumber" gem "database_cleaner" gem "cucumber-rails" end
bundle install to install all needed gems.
Use wanted generators
Then now edit the file
application.rb, located in the
config folder, and add the following lines:
# Configure generators values config.generators do |g| g.test_framework :rspec, :fixture => true g.fixture_replacement :factory_girl, :dir=>"spec/factories" end
This will make sure that the generators use our own defaults. So now when you type
rails generate model TestModel name:string rails generate controller TestModel
and it will create haml views, rspec tests, and factories. Awesome :)
Prepare to use jquery!
Download jquery, and make sure to include both in your application layout or view. In HAML it would look something like.
BUT: there is a quicker route! We also installed the gem
jqeury-rails, which has the following generator if we want to install jquery in one go!
rails g jquery:install #--ui to enable jQuery UI
Rails requires an authenticity token to do form posts back to the server. This helps protect your site against CSRF attacks. In order to handle this requirement the driver looks for two meta tags that must be defined in your page’s head. Luckily rails makes it easier for us, again, and we just need to include
csrf_meta_tag somewhere inside your page’s head (rails3 does this default in your
application.html.erb). Be sure to include it in your haml code too.
application.html.haml would look like this: