Ruby on Rails cheat sheet

Because I got tired of googling, I’ll list down some stuff I have been using on my Rails projects.

For more info on the Rails Command Line, check out Rails Guides – Rails Command Line.

To create a new rails app with PostgreSQL as the database

$ rails new app_name -d postgresql

Start the Postgres server:

$ pg_ctl start

Adding gems to your Gemfile:

$ bundle install

Make sure to run $ bundle update to update all of the dependencies to the latest versions:

$ bundle update

To access your rails app through the localhost. By default it’s at http://localhost:3000

$ rails s

To run the rails console to interact with your Rails app thru the command line:

$ rails c

Reload changes without having to do a restart

$ reload!


Using scaffold:

$ rails g scaffold Post name:string body:text

A scaffold in Rails is a full set of model, database migration for that model, controller to manipulate it, views to view and manipulate the data, and a test suite for each of the above.

Rails Guide1.3 bin/rails generate

To create a controller, the name should be: the first letter is capital, and in plural form.

$ rails g controller Posts

To create a controller with actions do:

$ rails g controller Posts index new edit show

To create a model, the name should be: the first letter is capital, and in singular form.

$ rails g model Post name:string body:text


To generate a migration file:

$ rails g migration CreateArticles name:string part_number:string

If you need to add or make changes as long as you haven’t run db:migrate. Go to schema.rb to make the changes otherwise you need to generate a new migration file.

$ rails g migration AddSomethingToArticles something:string

To run the migrations:

$ rails db:migrate

Sometimes things go wrong, for that “Oh shit!” moment.

To destroy the model article:

$ rails destroy model Article

To undo your most recent migration:

$ rails db:rollback


For more info on Rails Testing, check out Rails Guides -Testing Rails Applications.

To run every test to verify if it passes or not.

$rails test 

To run a specific test on a specific folder/location to verify if it passes or not.

$ rails test test/models/task_test.rb
$ rails test test/controllers/task_test.rb


To get a list of available routes in your application:

$ rails routes

To remove all old assets and rebuild them:

$ rails assets:clean && rails assets:precompile

Deploying your Rails app to Heroku

For more detailed instructions on how to deploy head on over to Heroku Dev Center – Deploying with Git

$ heroku login
$ heroku create
$ heroku rename <app-name>

$ git add .
$ git commit -m “commit message”
$ git push

$ git push heroku master
$ heroku run rails db:migrate
$ heroku open
Facebook Comments