Ruby on Rails cheatsheet

Cre­at­ing a new rails project

Using MySQL as the database

rails new myapp -d mysql

Using post­gres as the database

rails new myapp -d postgresql

Installing Gems

To man­u­ally install gems that you add, use the fol­low­ing com­mand from the project directory

bundle
# OR
bundle install

Start­ing your rails server

rails s
# OR
rails server

Visit http://localhost:3000/ to access the application.


Gen­er­a­tor commands

Rails gen­er­a­tor com­mands usu­ally start with rails generate. The short­hand for the same is rails g.

Gen­er­at­ing controllers

rails g controller controller_name action_name1 action_name2...
# e.g.
rails g controller Users index new create show update edit delete

Con­troller names are always plural

TODO: What do each of the action meth­ods stand for

Gen­er­at­ing models

rails g model SingularModelName field_name:datatype field_name:datatype
# e.g
rails g model User name:string email:string

Model names are always singular

Gen­er­at­ing migration

Cre­ate a new migra­tion file with a timestamp

rails g migration meaningfully_descriptive_name

If you want to cre­ate a migra­tion for a par­tic­u­lar table, then you can suf­fix it with to_tableName e.g.

rails g migration add_password_digest_to_users password_digest:string

Gen­er­at­ing inte­gra­tion tests (test for UI ele­ments like forms etc)

rails g integration_test testname
# e.g.
rails g integration_test users_signup

Routes

Check­ing the avail­able routes on your rails app

rake routes

Rake com­mands

Update your data­base based upon the lat­est migra­tion script

rake db:migrate

Run all the unit tests

rake test

Run spe­cific unit tests for cer­tain mvc folders

rake test:models
rake test:controllers
rake test:helpers

Rails con­sole

Rails let you run an inter­ac­tive con­sole which will already have access to your appli­ca­tion code. You can enter the rails con­sole through the fol­low­ing commands

rails console

By default rails con­sole accesses the ‘devel­op­ment’ envi­ron­ment. To enter the con­sole as another envi­ron­ment, use the following.

rails console test
rails console production
```sh

If you are already in the console, and you want to know which environment you are running against, you can do this
```sh
Rails.env
Rails.env.development?
Rails.env.test?
Rails.env.production?

You can also eval­u­ate the above vari­ables any­where in your code(e.g. in the view) if you need to do some­thing conditionally.


Run­ning the appli­ca­tion in dif­fer­ent modes

rails server --environment production
rails server --environment test
OR
RAILS_ENV=production rails server
RAILS_ENV=test rails server
rake db:migrate RAILS_ENV=production
rake db:migrate RAILS_ENV=test

When no envi­ron­ment is spec­i­fied, “devel­op­ment” is used as the default for all the commands.


Debug­ging

  • Place the key­word byebug any­where in the source code of your controllers/helpers/models and rails will auto­mat­i­cally pause exe­cute at that point.
  • To print the attribute val­ues of an object, use y user.attributes or puts user.attributes.to_yaml

Com­mon errors

model.save fails and returns false e.g

user.save
SELECT  1 AS one FROM `users` WHERE `users`.`email` = 'test@example.com' LIMIT 1
   (0.1ms)  ROLLBACK
 => false 

To exam­ine the errors, you can do

user.errors.messages

Ryan Sukale

Ryan is a UX engineer living in San Francisco, California.

You may also like...

  • Tanakorn Num­rub­porn

    Thank a bunch. This post help me a lot.