Controller Flashcards

1
Q

If you want a function to be executed for all your methods what can you add to the top of your controller file?

A

before_action :find_article

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

How would you find a model instance by id/attribute value?

A

Article.find(params[:id])

Article.find_by(title: ‘Gandalf’)

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

If you have a create action/method how can you create the model object to avoid the strong parameters security error? To tell Rails to only accept values from specific params hash keys?

A

def article_params
params[:article].permit(:title, :body)
end

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

How can you authenticate users to all actions for a controller? To make sure a user is logged in if they wish to create an article for example?

A

before_action :authenticate_user!

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

How can you update the attributes on an model instance?

A

Model.update(model_params)

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

What kind of variable do you use in order to access it from a view template?

A

An instance variable and you add a @ like so @article, now you have access to the variable in your view

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