Controller Flashcards
If you want a function to be executed for all your methods what can you add to the top of your controller file?
before_action :find_article
How would you find a model instance by id/attribute value?
Article.find(params[:id])
Article.find_by(title: ‘Gandalf’)
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?
def article_params
params[:article].permit(:title, :body)
end
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?
before_action :authenticate_user!
How can you update the attributes on an model instance?
Model.update(model_params)
What kind of variable do you use in order to access it from a view template?
An instance variable and you add a @ like so @article, now you have access to the variable in your view