We've looked at setting up the associations behind posts with comments, categories, and users. Now, let's actually give our users the ability to create new comments, users, and categories! For this lab, your models are already stubbed out, but you may need to update them to make your forms work properly.
- Construct a bi-directional has many through.
- Identify the join model in a has many through.
- Construct a nested params hash with data about the primary object and a has many through association.
- Use the conventional key names for associated data (association_attributes).
- Name form inputs correctly to create a nested params hash with has many through association data.
- Define a conventional association writer for the primary model to properly instantiate associations based on the nested params association data.
- Define a custom association writer for the primary model to properly instantiate associations with custom logic (like unique by name) on the nested params association data.
- Use fields_for to generate the association fields.
-
Create a form at
posts#new
to create a new post. The form should include check boxes for us to select categories as well as a text field to create a new category. You should do this with a nested form so that our controller stays thin. Also, typing in aCategory
name that already exists should not create a new category. Instead, if we type in a category that already exists, we should select it from the database and associate the post we've created with the existing category. -
Create a
posts#show
page to display the following: -
The title and content of the post.
-
All of the comments associated with the post.
-
A list of all the unique users who have commented on the post. A user's name should only show up once in this section, even if they've commented multiple times.
-
A form to add a new comment. The form should have a drop-down menu to select a user (we'll change this in future lessons to automatically associate the comment with a logged-in user). We should also be able to create a new user here and automatically associate it with the post.
-
Create a
users#show
page that links to all of the posts a user has commented on. -
Create a
categories#show
page that links to all of the posts associated with a given category.
- When creating new comments, we should only create a new user if they filled in that input.
accepts_nested_attributes_for
has areject_if
option that you can configure to only create new records if all the fields aren't blank. - Also, the select box that we use for the users needs to have a blank option –– check out the
include_blank
option forcollection_select
.
View Has Many Through Forms Rails Labs on Learn.co and start learning to code for free.