In our first class I was behind the ball in the first 5 minutes. Even remembering how to start up my test server was a distant memory from App Dev I. Throughout the class the material started to look familiar as the rust began to fall off.
To aid in my review I'm documenting building out the directors: model and sharing some learnings along the way.
Our goal
Develop a symetrical functionality to the movies: model for directors using idiomatic (at least as far as I know at this point...) Ruby.
Key Functions:
- Display an index of directors that links to each director's page
- Show each director's details on their own page
- Allow edits to each director
- Delete a director entry
Model
We can use rails g model Director <items> to generate the model for the new functionality.
Let's try
rails g model Director name dob style
We can then add some requirements to the database such as
validates :name, presence: true
validates :dob, presence: true
Now if we try to create a record in our database without these attributes filled in the action will fail and throw an error.
R(outes)
Starting in the routes file helps us to understand the functions that we will need to implement across the app.
We can use the shortcut in ruby resources :directors to generate all the idiomatic routes. This is equivalent to
get "/directors" => "directors#index"
# CREATE
post "/directors" => "directors#create", as: :directors
get "/directors/new" => "directors#new", as: :new_director
# READ
get "/directors" => "directors#index"
get "/directors/:id" => "directors#show", as: :director
# UPDATE
patch "/directors/:id" => "directors#update"
get "directors/:id/edit" => "directors#edit", as: :edit_director
# DELETE
delete "directors/:id" => "directors#destroy"
And before that we did this
get("/directors", { controller: "directors", action: "index" })
# CREATE
post("/directors", controller: "directors", action: "create")
get("/directors/new", controller: "directors", action: "new")
# READ
get("/directors", controller: "directors", action: "index")
get("/directors/:id", controller: "directors", action: "show")
# UPDATE
patch("/directors/:id", controller: "directors", action: "update")
get("/directors/:id/edit", controller: "directors", action: "edit" )
# DELETE
delete("/directors/:id", controller: "directors", action: "destroy")
C(ontroller)
After some tinkering here is my controller. I changed one thing which was redirecting to the individual page for each director after they are created.
class DirectorsController < ApplicationController
def new
@director = Director.new
end
def index
@directors = Director.all.order(:created_at, name: :desc)
respond_to do |format|
format.json do
render json: @directors
end
format.html
end
end
def show
@director = Director.find(params.fetch(:id))
end
def create
director_attributes = params.expect(director: [:name, :dob, :style])
@director = Director.new(director_attributes)
if @director.valid?
@director.save
redirect_to(director_url(@director.id), notice: "Director was successfully created.")
end
end
def edit
@director = Director.find(params.fetch(:id))
end
def update
director_attributes = params.expect(director: [:name, :dob, :style])
director = Director.find(params.fetch(:id))
director.name = params.fetch(:director).fetch(:name)
director.dob = params.fetch(:director).fetch(:dob)
director.style = params.fetch(:director).fetch(:style)
if director.valid?
director.save
redirect_to(director_url, notice: "Director updated successfully.")
else
redirect_to(director_url, alert: "Director failed to update successfully.")
end
end
def destroy
director = Director.find(params.fetch(:id))
director.destroy
redirect_to(directors_url, notice: "Director deleted successfully.")
end
end
A(ctions)
No actions yet!
V(iews)
View are quite similar to the views for movies so I'm omitting that for now!
Conclusion
And now everything seems to be working! It is certainly easier to work from the idiomatic Ruby, but it can be more confusing at times!
Top comments (0)