w4d1 Flashcards
What does the Rails router do?
Receives URLs and decides what to do with them (what paths are valid, what code to run, etc.).
What does #resources do in the routes.rb file?
Given the name of a (plural) model name as a symbol, it will generate 7 routes:
HTTP Verb
Path
action
used for
GET /photos index display a list of all photos
GET /photos/new new return an HTML form for creating a new photo
POST /photos create upload and create a new photo
GET /photos/:id show display a specific photo
GET /photos/:id/edit edit return an HTML form for editing a photo
PATCH or PUT /photos/:id update update a specific photo
DELETE /photos/:id destroy delete a specific photo
What is the convention for naming controllers?
The plural name of the model with the word “Controller” camel cased after it:
PhotosController, UsersController, etc.
How can an HTTP verb be specified in a url helper?
Add a method: argument:
button_to(photo_url(@photo), method: :delete)
What are some routing helper methods that controllers and views can use, in this case the ones that would be generated with resources :photos in routes.rb?
method
url
photos_url http://www.example-site.com/photos
new_photo_url http://www.example-site.com/photos/new
photo_url(@photo) http://www.example-site.com/photos/#{@photo.id}
edit_photo_url(@photo) http://www.example-site.com/photos/#{@photo.id}/edit
How can we embed query string options in url helpers?
photos_url(recent: true) == http://www.example-site.com/photos?recent=true
How can we specify the root of an app?
root to: ‘controllername#action’
In routes.rb
What is the command to see the available routes?
$rake routes
The following is part of the output from rake routes:
users GET /users(.:format) users#index
POST /users(.:format) users#create
What are the url helpers for these routes? Why are they the same.
You can tack on _url after the name of the left most column to get the routing helper. The helper for both of these is users_url; the router distinguishes between the two based on the HTTP verb that is sent.
What is the purpose of a controller?
After the router decides what controller to use, the controller is responsible for making sense of the request and producing the appropriate output.
It’s the controller’s job to ask the model layer to fetch data, to process user input to save new data, and to either build and send a response or redirect the user to a new path.
What does the router actually do once it decides on a controller?
It instantiates an instance of that controller and calls the specified action (method) on that instance.
Where do controller actions usually get their data from?
From the model class and its instances.
Where does the data for ‘params’ come from?
Parsed from the query string of a URL, and/or from an HTML form.
What method actually gets the params hash?
ActionController::Base#params
How is #permit used to create strong parameters?
It “whitelists” parameters that can be changed. Non-whitelisted attributes cannot be changed:
@post = Post.new(params[:post].permit(:title, :body))