• 0

Ruby on Rails cheatsheet

Creating a new rails project

Using MySQL as the database
rails new myapp -d mysql
Using postgres as the database
rails new myapp -d postgresql

Installing Gems

To manually install gems that you add, use the following command from the project directory
bundle
# OR
bundle install

Starting your rails server

rails s
# OR
rails server
Visit http://localhost:3000/ to access the application.

Generator commands

Rails generator commands usually start with rails generate. The shorthand for the same is rails g.

Generating controllers

rails g controller controller_name action_name1 action_name2...
# e.g.
rails g controller Users index new create show update edit delete
Controller names are always plural
TODO: What do each of the action methods stand for

Generating 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

Generating migration

Create a new migration file with a timestamp
rails g migration meaningfully_descriptive_name
If you want to create a migration for a particular table, then you can suffix it with to_tableName e.g.
rails g migration add_password_digest_to_users password_digest:string

Generating integration tests (test for UI elements like forms etc)

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

Routes

Checking the available routes on your rails app
rake routes

Rake commands

Update your database based upon the latest migration script
rake db:migrate
Run all the unit tests
rake test
Run specific unit tests for certain mvc folders
rake test:models
rake test:controllers
rake test:helpers

Rails console

Rails let you run an interactive console which will already have access to your application code. You can enter the rails console through the following commands
rails console
By default rails console accesses the 'development' environment. To enter the console as another environment, 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 evaluate the above variables anywhere in your code(e.g. in the view) if you need to do something conditionally.

Running the application in different 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 environment is specified, "development" is used as the default for all the commands.

Debugging

  • Place the keyword byebug anywhere in the source code of your controllers/helpers/models and rails will automatically pause execute at that point.
  • To print the attribute values of an object, use y user.attributes or puts user.attributes.to_yaml

Common errors

model.save fails and returns false e.g
user.save
SELECT  1 AS one FROM `users` WHERE `users`.`email` = '[email protected]' LIMIT 1
   (0.1ms)  ROLLBACK
 => false
To examine the errors, you can do
user.errors.messages