Challenge - 5 Problems
Rails Forms Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
What happens when a Rails form is submitted without a CSRF token?
Consider a Rails form that submits data to the server. What will happen if the form does not include the CSRF authenticity token?
Ruby on Rails
<%= form_with url: '/submit', method: :post do %> <%= text_field_tag :name %> <%= submit_tag 'Send' %> <% end %>
Attempts:
2 left
💡 Hint
Rails protects forms from cross-site request forgery by requiring a token.
✗ Incorrect
Rails includes a CSRF token in forms to protect against unauthorized requests. If the token is missing or invalid, the server raises an InvalidAuthenticityToken error to block the request.
❓ state_output
intermediate2:00remaining
What is the value of params[:email] after submitting this form?
Given the following Rails form, what will be the value of params[:email] in the controller after submission if the user types 'user@example.com'?
Ruby on Rails
<%= form_with url: '/subscribe', method: :post do %> <%= email_field_tag :email %> <%= submit_tag 'Subscribe' %> <% end %>
Attempts:
2 left
💡 Hint
Check how email_field_tag sets the parameter name.
✗ Incorrect
Using email_field_tag with a symbol :email creates a form input named 'email'. When submitted, params[:email] contains the string entered by the user.
📝 Syntax
advanced2:30remaining
Which option correctly creates a form with nested attributes in Rails?
You want to create a form for a Post model that accepts nested attributes for comments. Which code snippet correctly sets up the form fields for comments?
Attempts:
2 left
💡 Hint
fields_for expects the association name as a symbol.
✗ Incorrect
The correct syntax uses f.fields_for with the association symbol :comments. Option C uses singular :comment, B misses the receiver 'f.', and C passes a collection instead of symbol.
🔧 Debug
advanced2:30remaining
Why does this Rails form raise a 'NoMethodError' for 'submit_tag'?
Examine the code below. Why does it raise a NoMethodError for 'submit_tag'?
Ruby on Rails
<%= form_with url: '/create', method: :post do |f| %> <%= f.text_field :name %> <%= submit_tag 'Create' %> <% end %>
Attempts:
2 left
💡 Hint
Check how submit buttons are created inside form builders.
✗ Incorrect
Inside form_with with a block variable 'f', you should use f.submit to create the submit button. submit_tag is a standalone helper used outside form builders. Using submit_tag inside the block without prefix causes NoMethodError.
🧠 Conceptual
expert3:00remaining
Why are forms central to user interaction in Rails applications?
Select the best explanation for why forms are crucial in driving user interaction in Rails web apps.
Attempts:
2 left
💡 Hint
Think about how users communicate their intentions to the app.
✗ Incorrect
Forms let users enter and submit data in a structured way. This data is sent to the server, which processes it and updates the app state or content. This interaction is fundamental to dynamic web apps.