0
0
Ruby on Railsframework~20 mins

Form helpers (form_with) in Ruby on Rails - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Form Helpers Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the output HTML of this form_with helper?
Consider this Rails view code:
form_with(model: @post, local: true) do |form|
  concat form.label :title
  concat form.text_field :title
  concat form.submit 'Save'
end

What HTML does this generate if @post is a new record?
Ruby on Rails
form_with(model: @post, local: true) do |form|
  concat form.label :title
  concat form.text_field :title
  concat form.submit 'Save'
end
A
<form action="/posts" method="post">
  <label for="post_title">Title</label>
  <input type="text" name="post[title]" id="post_title" />
  <input type="submit" value="Save" />
</form>
B
<form action="/posts/1" method="post">
  <label for="post_title">Title</label>
  <input type="text" name="post[title]" id="post_title" />
  <input type="submit" value="Save" />
</form>
C
<form action="/posts" method="get">
  <label for="post_title">Title</label>
  <input type="text" name="post[title]" id="post_title" />
  <input type="submit" value="Save" />
</form>
D
<form action="/posts/1" method="patch">
  <label for="post_title">Title</label>
  <input type="text" name="post[title]" id="post_title" />
  <input type="submit" value="Save" />
</form>
Attempts:
2 left
💡 Hint
Think about what URL and HTTP method Rails uses for new records with form_with.
state_output
intermediate
2:00remaining
What is the value of the hidden input generated by form_with for an existing record?
Given this code snippet:
form_with(model: @post, local: true) do |form|
  concat form.hidden_field :id
end

If @post.id is 42, what is the value attribute of the hidden input?
Ruby on Rails
form_with(model: @post, local: true) do |form|
  concat form.hidden_field :id
end
A<input type="hidden" name="post[id]" id="post_id" value="" />
B<input type="hidden" name="post[id]" id="post_id" value="42" />
C<input type="hidden" name="post[id]" id="post_id" value="0" />
D<input type="hidden" name="post[id]" id="post_id" />
Attempts:
2 left
💡 Hint
Hidden fields reflect the model's attribute values.
📝 Syntax
advanced
2:00remaining
Which form_with syntax correctly disables remote AJAX submission?
You want to create a form that submits synchronously (not via AJAX). Which option correctly disables remote submission?
Aform_with(model: @user, remote: false) do |form| ... end
Bform_with(model: @user, submit_via_ajax: false) do |form| ... end
Cform_with(model: @user, ajax: false) do |form| ... end
Dform_with(model: @user, local: true) do |form| ... end
Attempts:
2 left
💡 Hint
Check the official Rails option for disabling remote forms.
🔧 Debug
advanced
2:00remaining
Why does this form_with raise an error?
Given this code:
form_with(url: posts_path, method: :post) do |form|
  concat form.text_field :title
  concat form.submit
end

Why does this raise an error?
Ruby on Rails
form_with(url: posts_path, method: :post) do |form|
  concat form.text_field :title
  concat form.submit
end
ABecause <code>form.text_field</code> requires a model object to infer the field name.
BBecause <code>method: :post</code> is invalid; it should be a string.
CBecause <code>form.submit</code> must have a label argument.
DBecause <code>url</code> option cannot be used with <code>form_with</code>.
Attempts:
2 left
💡 Hint
Think about what form.text_field needs to generate the input name.
🧠 Conceptual
expert
3:00remaining
How does form_with decide the HTTP method for an existing record?
When you use form_with(model: @article) and @article is persisted, which HTTP method does Rails use and why?
ARails uses PUT because it replaces the entire resource.
BRails uses POST because it always submits forms with POST.
CRails uses PATCH because it updates the existing record partially.
DRails uses GET because it fetches the resource for editing.
Attempts:
2 left
💡 Hint
Think about RESTful conventions for updating resources.