w4d2 - rails Flashcards

1
Q

What’s the naming convention for controllers?

A

plural and CamelCase

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

What’s the naming convention for views?

A

[name of action].html.erb

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

What does erb stand for?

A

Embedded RuBy

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

What’s the difference between the two erb ruby tags?

A

The one with the equals will be rendered. The one without will not be rendered.

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

What’s the name of the parent layout?

A

application.html.erb

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

What are url helper methods? Where are they defined? How do you use them?

A

Shortcuts for the url that will point to a particular action.

They are defined in routes.rb and can be seen via ‘rails routes’.

You use them as if they are a method, so:

redirect_to whatever_url

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

What causes a DoubleRenderError? What’s one way to get around it?

A

The cause is assuming that methods like redirect_to and render act like returns, and then using these multiple times in an action.

To get around it, place a return statement after using a conditional redirect_to/render

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

How do you specify which url and which method a form will submit to?

A

form action=”url” method=”post/get”

For PATCH/PUT:

Use the method above with ‘post’ AND THEN add a hidden input like so:

input type=”hidden” name=”_method” value=”PATCH/PUT”

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

What’s the general pattern for create?

A

Call new on a model, passing in params.

Try to save it. If it saves, we show user the show view. If it fails, we show the user the create view.

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

When first starting out, how do you allow rails to accept forms without auth tokens?

A

Comment out the protect_from_forgery line in application_controller.rb

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

How do you render a partial from inside of a view?

A

render ‘partial_name’, var1: :value1, var2: :value2, …

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

What’s a shortcut to render a partial for each model in a collection?

What needs to be set up for this to work?

A

render @mymodels

There must be a model named ‘mymodel’ AND there must be a partial named ‘_mymodel’

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

What’s the name of the gem that gives a REPL in the browser with the ‘fail’ keyword?

A

better_errors

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

What are the 3 places rails params come from?

A

query params
route URI/wildcards
POST/PATCH request data

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

What are the 5 aspects of a HTTP request?

A
method
path
query string
header fields
body
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What do these 5 things add up to?

method
path
query string
header fields
body
A

a HTTP request

17
Q

What’s the difference between PUT/PATCH?

A

PUT sends the entire model through, regardless of what changed.

PATCH only sends the changed parts

18
Q

What are the 5 categories of HTTP status codes?

A
1xx - informational
2xx - success
3xx - redirection
4xx - client errors
5xx - server errors
19
Q

What does the request/response lifecycle of a rails app look like?

A

browser sends request to server
server receives request
router instantiates correct controller instance
controller receives params, instantiates models
models query database
controller handles logic and renders view
controller returns view back to server
server wraps view in HTTP headers
server hands response to client

20
Q

What does the router do?

A

Takes in a method/path and instantiates an appropriate controller and invokes an appropriate method

21
Q

What does a simple route look like for tweets?

Assume the presence of a TweetsController.

A

get ‘/tweets’, to: ‘tweets#index’

22
Q

What are the 4 parts of a route?

A

method
path
controller
action

23
Q

method
path
controller
action

These combine to form what?

A

A route

24
Q

How do you use render to show a JSON object?

A

render :json object

25
Q

What does this do:

render :json object

A

Renders a JSON object to the client

26
Q

What are the 5 standard RESTful methods and what do they do?

1 star per

A
index - shows all items
show - shows one item
create - makes a new object
update - changes an existing object
destroy - deletes an object
27
Q

RESTfully, where do we post things in order to create them?

A

POST /index

28
Q

RESTfully, what’s the proper create method look like?

A
  1. Create a new model instance based on POST params.
  2. if we can save this model, render/redirect to success page
    else: render an error
29
Q

What are weak parameters? How does rails guard against it? What is it called?

A

You can’t create a new model like this:

Thing.new(params)

You have to use

params.require(:thing).permit(:attribute1, :attribute2, …)

This is called strong parameters

30
Q

What is the :id part of /index/:id called?

A

wildcard

31
Q

What’s the shortcut for creating RESTful routes?

A

in routes.rb:

resources :plural_model

32
Q

How do you update multiple attribute for a model at once?

A

model.update_attributes(params)

33
Q

why or why shouldn’t you use instance vars in partials

A

you can, but you shouldn’t.

Failing to provide a required instance variable can produce confusing errors, rather than the unambiguous”no local variable defined”error raised when a local variable is undefined.