Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to allow nested attributes for comments in the Post model.
Ruby on Rails
class Post < ApplicationRecord has_many :comments accepts_nested_attributes_for :[1] end
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using the wrong association name in accepts_nested_attributes_for.
Forgetting to add accepts_nested_attributes_for altogether.
✗ Incorrect
The method accepts_nested_attributes_for is used to allow nested attributes for associated models. Here, it should be used with :comments to enable nested attributes for comments in the Post model.
2fill in blank
mediumComplete the form helper to build fields for nested comments inside the post form.
Ruby on Rails
<%= form_with model: @post do |f| %>
<%= f.text_field :title %>
<%= f.fields_for :[1] do |c| %>
<%= c.text_area :content %>
<% end %>
<% end %> Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using the wrong symbol in fields_for causing no nested fields to appear.
Not matching the association name exactly.
✗ Incorrect
fields_for is used to create form fields for nested attributes. It should match the association name, which is :comments in this case.
3fill in blank
hardFix the error in the controller to permit nested comment attributes.
Ruby on Rails
def post_params params.require(:post).permit(:title, comments_attributes: [:id, :content, :[1]]) end
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Not permitting :_destroy causes nested record deletion to fail.
Permitting unrelated keys that do not affect nested attributes.
✗ Incorrect
The :_destroy key is used to allow deletion of nested records through nested attributes. It must be permitted in strong parameters to work.
4fill in blank
hardFill both blanks to build a nested attributes hash for a new comment with content and mark it for destruction.
Ruby on Rails
post_attributes = {
title: 'New Post',
comments_attributes: {
'0' => { content: [1], [2]: true }
}
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using the wrong key name instead of :_destroy.
Putting a non-string value for content.
✗ Incorrect
The content key should have a string value for the comment text. The special key :_destroy with value true marks the nested comment for deletion.
5fill in blank
hardFill all three blanks to create a nested attributes hash for updating a comment's content and allowing its destruction.
Ruby on Rails
post_params = {
post: {
title: 'Updated Post',
comments_attributes: {
'0' => { id: [1], content: [2], [3]: true }
}
}
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a string instead of a number for id.
Forgetting to include :_destroy to allow deletion.
✗ Incorrect
The id should be the comment's id (number). The content is a string with the new comment text. The :_destroy key with true allows marking the comment for deletion.