0
0
Ruby on Railsframework~20 mins

Strong parameters in Ruby on Rails - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Strong Parameters Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the output of this controller action with strong parameters?
Consider this Rails controller code snippet. What will be the value of @user after create is called with params containing { user: { name: 'Alice', admin: true } }?
Ruby on Rails
class UsersController < ApplicationController
  def create
    @user = User.new(user_params)
  end

  private

  def user_params
    params.require(:user).permit(:name)
  end
end
A@user will have name 'Alice' and admin set to nil
B@user will have name nil and admin set to true
C@user will have name 'Alice' and admin set to true
DAn error will be raised because admin is not permitted
Attempts:
2 left
💡 Hint
Think about which parameters are allowed by permit.
📝 Syntax
intermediate
2:00remaining
Which option correctly permits nested attributes with strong parameters?
You want to permit a post with nested comments attributes in your controller. Which post_params method is correct?
Aparams.require(:post).permit(:title, comments: [:content, :author])
Bparams.require(:post).permit(:title, comments_attributes: [:content, :author])
Cparams.require(:post).permit(:title, :comments => [:content, :author])
Dparams.require(:post).permit(:title, comments_attributes: :content, :author)
Attempts:
2 left
💡 Hint
Remember the naming convention for nested attributes in Rails forms.
🔧 Debug
advanced
2:00remaining
Why does this strong parameters code fail to update the article?
Given this code, why does calling update fail to update the article attributes?
Ruby on Rails
def update
  @article = Article.find(params[:id])
  @article.update(article_params)
end

def article_params
  params.permit(:title, :body)
end
ABecause <code>article_params</code> should use <code>params.require(:article).permit(:title, :body)</code>
BBecause <code>params</code> does not require the <code>:article</code> key, so <code>permit</code> is called on the wrong level
CBecause <code>params</code> is not permitted at all
DBecause <code>permit</code> is missing the <code>:id</code> parameter
Attempts:
2 left
💡 Hint
Think about how nested parameters are structured in Rails forms.
state_output
advanced
2:00remaining
What is the value of params[:user] after this strong parameters call?
Given this controller snippet, what will params[:user] contain after user_params is called?
Ruby on Rails
def user_params
  params.require(:user).permit(:name, :email)
end

# Assume params = { user: { name: 'Bob', email: 'bob@example.com', admin: true } }
ARaises an error because admin is not permitted
B{ name: 'Bob', email: 'bob@example.com' }
C{ name: 'Bob', email: 'bob@example.com', admin: true }
DUnchanged original params hash
Attempts:
2 left
💡 Hint
Strong parameters return a filtered copy, but do not modify the original params hash.
🧠 Conceptual
expert
2:00remaining
Which statement best describes the purpose of strong parameters in Rails?
Choose the most accurate explanation of why Rails uses strong parameters.
ATo convert all parameters to strings for consistent processing
BTo automatically validate all incoming parameters against the database schema
CTo prevent mass assignment vulnerabilities by explicitly allowing only permitted attributes
DTo encrypt sensitive parameters before saving them to the database
Attempts:
2 left
💡 Hint
Think about security risks when accepting user input in web apps.