When creating a Ruby on Rails project it is always easiest when you have a checklist to refer too incase we don’t miss any little details. So that is what we are going to do today! A Ruby on Rails Checklist!
1.Take a few minutes writing out a domain model (always easiest on a whiteboard or piece of paper):
What relationships you will have (has many? belongs to?)
What attributes each model will have.
What you will use in the controller and views.
2) If you are starting from scratch then type “Rails new name_of_application” and this will generate a lot of flies to get you started and set up in your Rails application.
3) Keep up a schema and routes to always check tables
4) Type in the Terminal: Rails g model Artist name age:integer img —no-test-framework
- (model is always singular) (adding string is not necessary)
5) Write your Relationships:
- has_many :performances (plural)
has_many :cities, through: :performances
——————————————————has_many :performances
has_many :artists, through: : performances
6) Other examples:
Rails g model flight air_type:integer time:datetime num_passengars:integer destination
Rails g model passenger name passport:boolean suitcases:integer
Rails g model ticket date:datetime seat_num:integer passenger_id:integer flight:references
7) Check migration: and then type in the Terminal: rails db:migrate
8) Check schema for what you need to for seeds. Examples:
a1 = Artist.create(name:"50 cent",bio:"i'm a rapper”)
c1 = Cities.create(name:”Auckland")
p1 = Performances.create(name: “Indi Feast", artist_id: a1.id, cities_id: c1.city_id)
Also make sure that you have created your artist and city seeds before creating performances
9) Type in the Terminal: rails db:seed
10) Create the controllers by typing in the terminal:
Rails g controller passengers(plural) new create show edit
Rails g controller flights(plural)
Rails g controller tickets(plural)
The “new create show edit” will create actions in your controller and view files for each of them.
An example of a controller is:
class ArtistsController < ApplicationController
before_action :find_artist, only: [:show]
def index
@artists = Artist.all
end
def show
end
def new
@artist = Artist.new
end
def create
@artist = Artist.new(artist_params)
if @artist.save
redirect_to artist_path(@artist)
else
render :new
end
end
def edit
@artist = Artist.find(params[:id])
end
def update
If @artist.update(artist_params)
redirect_to cities_path(@artist.city)
else
render :edit
end
end
def destroy
@artist = Artist.find(params[:id]).destroy
redirect_to cities_path
end
private
def artist_params
params.require(:artist).permit(:name, :age, :img)
end
def find_artist
@artist = Artist.find(params[:id])
end
end
11) Go back and update your routes for every action that you need (1 resource route per model) i.e. resources :posts, only: [:index, :new, :create] OR get '/post/:id', to: posts#show', as: 'post'. Join table is always first in the routes.rb
12) Once your controllers are done set up your view files.
13) Type in the terminal rails s and then open up your localhost website to see if everything is working. If an error occur the web sever will tell you. These error messages are very clear and easy to follow.
Helpful Websites:
You can also add Validations. A great Website for this is: https://guides.rubyonrails.org/active_record_validations.html
For Routing and controllers: https://dev.to/morinoko/the-basics-of-sinatra--la-mvc--configuration--routes--forms-417e &&& https://guides.rubyonrails.org/routing.html
For Render and Redirect: https://medium.com/@kerenlerner/render-and-redirect-which-to-use-106ff653ee9a
Top comments (0)