0
0
Ruby on Railsframework~5 mins

Strong parameters in Ruby on Rails - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What are strong parameters in Rails?
Strong parameters are a way to whitelist which attributes can be passed from a form to the controller, helping to prevent unwanted or harmful data from being saved.
Click to reveal answer
beginner
Why do we need to use strong parameters in Rails controllers?
To protect the app from mass assignment vulnerabilities by explicitly allowing only permitted attributes to be updated or created.
Click to reveal answer
beginner
How do you permit a parameter named 'title' inside a strong parameters method?
You use params.require(:model_name).permit(:title) to allow the 'title' attribute.
Click to reveal answer
intermediate
What happens if you try to save a parameter that is not permitted by strong parameters?
Rails will ignore the unpermitted parameter and it will not be saved or updated in the database.
Click to reveal answer
beginner
Show a simple example of a strong parameters method for a 'Post' model with 'title' and 'content' attributes.
def post_params params.require(:post).permit(:title, :content) end
Click to reveal answer
What is the main purpose of strong parameters in Rails?
ATo whitelist allowed attributes from user input
BTo speed up database queries
CTo style forms automatically
DTo generate HTML views
Which method is used to specify required parameters in strong parameters?
Apermit
Ballow
Cfilter
Drequire
What will happen if you omit strong parameters in a Rails controller when saving data?
AAll parameters will be saved automatically
BThe database will be deleted
CRails will raise an error or ignore unpermitted parameters
DThe app will crash immediately
How do you permit multiple attributes like 'title' and 'content' in strong parameters?
Apermit(:title, :content)
Brequire(:title, :content)
Callow(:title, :content)
Dfilter(:title, :content)
Which of these is a correct strong parameters method for a 'User' model with 'name' and 'email'?
Aparams.permit(:user).require(:name, :email)
Bparams.require(:user).permit(:name, :email)
Cparams.allow(:user).permit(:name, :email)
Dparams.require(:name, :email).permit(:user)
Explain in your own words why strong parameters are important in Rails applications.
Think about how user input can affect your app's data.
You got /4 concepts.
    Write a strong parameters method for a 'Comment' model that permits 'author' and 'body' attributes.
    Use require and permit methods correctly.
    You got /2 concepts.