0
0
Ruby on Railsframework~20 mins

Model-backed forms in Ruby on Rails - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Model-backed Forms Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What happens when a model-backed form is submitted with invalid data?

Consider a Rails form built with form_with model: @user. If the user submits the form with invalid data that fails model validations, what is the typical behavior?

AThe server crashes with an unhandled exception.
BThe form clears all fields and redirects to the homepage without showing errors.
CThe form submits successfully and saves the invalid data to the database.
DThe form re-renders with error messages displayed, and the invalid data remains in the form fields.
Attempts:
2 left
💡 Hint

Think about how Rails handles validation failures in controller actions.

📝 Syntax
intermediate
2:00remaining
Which option correctly creates a model-backed form for a new Post?

Choose the correct Rails code snippet to create a form for a new Post model instance using form_with.

A<%= form_with model: @post, local: true do |form| %> ... <% end %>
B<%= form_with url: posts_path, method: :get do |form| %> ... <% end %>
C<%= form_with model: Post.new, remote: true do |form| %> ... <% end %>
D<%= form_for @post do |form| %> ... <% end %>
Attempts:
2 left
💡 Hint

Remember that form_with is the modern helper replacing form_for, and local: true disables AJAX.

state_output
advanced
2:00remaining
What is the value of @post.title after submitting the form with title 'Hello' but validation fails?

Given a Post model with a validation that title must be unique, and a form submits with title 'Hello' which already exists, what will @post.title be in the controller after @post.update(post_params) fails?

Ruby on Rails
def update
  @post = Post.find(params[:id])
  if @post.update(post_params)
    redirect_to @post
  else
    render :edit
  end
end
AThe original title before update
Bnil
C'Hello'
DRaises an error
Attempts:
2 left
💡 Hint

Think about how Rails assigns attributes before validation.

🔧 Debug
advanced
2:00remaining
Why does the form not show validation errors after submission?

A developer uses form_with model: @article but after submitting invalid data, the form reloads without showing any error messages. What is the most likely cause?

AThe controller action does not re-render the form view on validation failure.
BThe model has no validations defined.
CThe form uses <code>form_with</code> without a block.
DThe form is missing the <code>authenticity_token</code>.
Attempts:
2 left
💡 Hint

Check what the controller does when saving fails.

🧠 Conceptual
expert
3:00remaining
How does Rails link form fields to model attributes in model-backed forms?

In a Rails model-backed form, how does Rails know which form input corresponds to which model attribute when the form is submitted?

ARails matches input <code>id</code> attributes to model attribute names automatically.
BRails uses the <code>name</code> attribute of inputs, formatted as <code>model_name[attribute]</code>, to map submitted data to model attributes.
CRails uses JavaScript to bind inputs to model attributes dynamically at runtime.
DRails relies on the order of inputs in the form to assign values to model attributes.
Attempts:
2 left
💡 Hint

Think about how HTML form data is structured and sent to the server.