Skip to content

Instantly share code, notes, and snippets.

@epoch
Last active November 21, 2019 11:57
Show Gist options
  • Save epoch/576274969497a2339928 to your computer and use it in GitHub Desktop.
Save epoch/576274969497a2339928 to your computer and use it in GitHub Desktop.
Plurals or Singular

1. Create new rails app

named my_awesome_app with postgresql -d=postgresql but skip making a git repository -G & tests boilerplate -T:

rails new my_awesome_app -d=postgresql -GT
cd my_awesome_app

2. Create the database based on default settings in database.yml

rails db:create

3. use generators to generate boilerplate:

commonly used generators are the resource generator, model generator & migration generator

resource generator example

rails generate resource product name:string description:text

generate a model, a controller and a migration file boilerplate

model generator example

rails generate model product name:string description:text  

generate a model and a migration file boilerplate

migration generator example

rails generate migration add_user_id_to_products user:references  

generate a migration file that adds a foreign key user_id to the products table

4. run migrations to update database schema

rails db:migrate

controller name and view folder name are based on your route

5. given the following route:

get '/things', to: 'products#index'

the controller name will be products, the method inside the controller will be index, the index.html.erb will be in a sub folder named products and the controller filename will be products_controller.rb

class ProductsController < ApplicationController

  def index
  end

end

view template for index /views/products/index.html.erb

model file product.rb and filename: always singular

class Product < ApplicationRecord
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment