w4d1 Flashcards

1
Q

What does the Rails router do?

A

Receives URLs and decides what to do with them (what paths are valid, what code to run, etc.).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What does #resources do in the routes.rb file?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is the convention for naming controllers?

A

The plural name of the model with the word “Controller” camel cased after it:

PhotosController, UsersController, etc.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

How can an HTTP verb be specified in a url helper?

A

Add a method: argument:

button_to(photo_url(@photo), method: :delete)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

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?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

How can we embed query string options in url helpers?

A

photos_url(recent: true) == http://www.example-site.com/photos?recent=true

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

How can we specify the root of an app?

A

root to: ‘controllername#action’

In routes.rb

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is the command to see the available routes?

A

$rake routes

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

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.

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is the purpose of a controller?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What does the router actually do once it decides on a controller?

A

It instantiates an instance of that controller and calls the specified action (method) on that instance.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Where do controller actions usually get their data from?

A

From the model class and its instances.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Where does the data for ‘params’ come from?

A

Parsed from the query string of a URL, and/or from an HTML form.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What method actually gets the params hash?

A

ActionController::Base#params

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

How is #permit used to create strong parameters?

A

It “whitelists” parameters that can be changed. Non-whitelisted attributes cannot be changed:

@post = Post.new(params[:post].permit(:title, :body))

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Can you use a nested hash to create or update a model instance? Why/why not?

A

You cannot do so in the controller because of strong parameter limitations.

17
Q

What should strong parameter helper methods be?

A

Private/protected:

def cat_params
params.require(:cat).permit(:name, :age)
end

18
Q

How do we tell the controller what object we are talking about?

A

With :id

def update

@cat = Cat.find(params[:id])
@cat.update!(cat_params)
redirect_to cat_url(@cat)   end
19
Q

What does the controller instance do once it has been told what action to call?

A

It either renders a response (view) or issues a redirect.

20
Q

What happens after the controller instance issues a response?

A

The request is over and the connection between client-and-server is closed. The controller instance is discarded.

21
Q

What happens to instance variables stored in the controller instance?

A

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.

22
Q

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?

A

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.

23
Q

Describe the basic relationship between Rails and JSON.

A

The model layer converts a model to JSON, and the controller layer returns the JSON to the user.

24
Q

What should all controller actions end with?

A

Either #render or #redirect_to

25
Q

Is the following controller code valid:

render json: users

A

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.

26
Q

Given the following association, what would the routing that reflects this look like?

# app/models/magazine.rb
class Magazine
A

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