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))
Can you use a nested hash to create or update a model instance? Why/why not?
You cannot do so in the controller because of strong parameter limitations.
What should strong parameter helper methods be?
Private/protected:
def cat_params
params.require(:cat).permit(:name, :age)
end
How do we tell the controller what object we are talking about?
With :id
def update
@cat = Cat.find(params[:id]) @cat.update!(cat_params) redirect_to cat_url(@cat) end
What does the controller instance do once it has been told what action to call?
It either renders a response (view) or issues a redirect.
What happens after the controller instance issues a response?
The request is over and the connection between client-and-server is closed. The controller instance is discarded.
What happens to instance variables stored in the controller instance?
Setting instance variables in the controller does not affect the processing of future requests. State is saved either in the database (server-side) or the cookie (client-side). Since instance variables will be lost (in fact the whole controller object is lost) immediately after the response is issued, you cannot use instance-variable data stored from previous requests.
Why would non-browser program prefer to have responses to their HTTP requests be delivered in a form like JSON instead of something like HTML?
JSON and similar formats are lightweight and only contain data the program needs, as opposed to the extraneous display information that abounds in HTML. Requests like these are known as API requests.
Describe the basic relationship between Rails and JSON.
The model layer converts a model to JSON, and the controller layer returns the JSON to the user.
What should all controller actions end with?
Either #render or #redirect_to
Is the following controller code valid:
render json: users
Yes, because render merely places something in the response body. If this is part of a JSON API for a non-browser, the JSON representation will suffice.
Given the following association, what would the routing that reflects this look like?
# app/models/magazine.rb class Magazine
config/routes.rb
NewspapersApp::Application.routes.draw do
resources :magazines do
# provides a route to get all the articles for a given magazine.
resources :articles, only: :index
end
# provides all seven typical routes
resources :articles
end