Skip to content

Instantly share code, notes, and snippets.

@goutham2027
Last active August 29, 2015 14:20
Show Gist options
  • Save goutham2027/bb93c7c5726e69a3efdc to your computer and use it in GitHub Desktop.
Save goutham2027/bb93c7c5726e69a3efdc to your computer and use it in GitHub Desktop.
Ruby on Rails Cheat Sheet

Naming Convention

Rule Class name Filename
All models are singular Note note.rb
All controllers are plural Notes notes_controller.rb
View folder names are plural - notes/index.html.haml

Rails CLI commands

rails new <project-name> # create a new project
rails generate model <model_name_singular> <optional-fields>:<field-type>
rails generate controller <controller_name_plural> <optional-methods>
rails generate migration <migration-name> <optional-fields>
Ex: rails g migration AddNameToUser name:string
rake db:migrate # to migrate db
rake db:rollback # to rollback

Model Field types:

  • :primary_key
  • :string, :text
  • :integer, :float, :decimal
  • :datetime, :time, :date
  • :binary, :boolean.

Routing

config file - config/routes.rb

root to: "controller#action"
resources :<controller_name_plural>
eg: resources :notes
resource :<controller_name_singular>
eg: resource :session, only: [:create, :destroy]

Rake

Path: lib/tasks
<task_name>.rake
lib/tasks  // Path to rake tasks
$ rake -T  // To list all rake tasks
$ rake db:seed // To seed data to db, lives in db/seeds.rb

Rspec

Path: spec/models, spec/conntroller
spec file should end with _spec.rb 
Eg: notes_controller_spec.rb
$ rails generate rspec:install
$ rails g rspec:model <model_name>
$ rails g rspec:controller <controller_name>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment