Being new to Rails, I am having a difficult time finding a website or reference that gives a run down summary of Ruby on Rails. I understand MVC, ActiveRecord, and that sort of
I wrote some naming conventions some time ago:
All filenames are in snake_case following the same conventions
- Model: singular (e.g. Restaurant
)
- Controller: plural (e.g. RestaurantsController
)
- Table in DB: plural (e.g. restaurants
)
- URL's: all in plural (e.g. /restaurants
, /restaurants/:id
, /restaurants/new
)
rails generate
Commandsrails g model
Restaurant
name:string rating:integer
rails g migration AddDescriptionTo
Restaurants
description:text
rails g controller
Restaurants
index show
ActiveRecord
methodsAll in singular, because all ActiveRecord
's methods are linked to the model.
Examples:
- Restaurant.all
- Restaurant.create(name: "Burger King", rating: 2)
- Restaurant.find(params[:id])
belongs_to
. Because it belongs to one element.has_many
. Because it has many elements.e.g.
class Restaurant < ActiveRecord::Base
has_many :reviews
end
class Review < ActiveRecord::Base
belongs_to :restaurant
end
Plural when defining a route for a resource:
resources :restaurants
index
: plural (because we are showing a list of elements). e.g. restaurants_path
. Can also be used used for create
.show
: singular (we are showing just one element, it needs the element inside parenthesis). e.g. restaurant_path(@restaurant)
. Can also be used for update
& delete
.new
: singular. e.g. new_restaurant_path