We're going to build off the previous iteration of our Blog App, where we created new models for User
and Tag
(and applicable associations) and wrote validations. We want to clean up our tagging feature. Our ability to add tags to a new post is super useful, but what if, when we're making a new post, we want to add a new tag that isn't in the list? Let's build that out.
NOTE: As with much of our Rails curriculum, remember to always use the --no-test-framework
flag when you generate models, controllers, etc. That way, the Rails generators will not create additional tests on top of the test suite that already comes with the lesson. E.g., rails g model User username:string email:string --no-test-framework
. However, it is not needed for this lab as we've provided the starter files.
- We need to change the permitted params in our
PostsController
to accept another attribute,:tags_attributes
, which contains the tag attributes that we need to create a new tag. - We also need an
accepts_nested_attributes_for
macro on ourPost
model, which will permit tags to be nested in our newPost
form. - Now we can build a nested form in our
Post
form. Check out the documentation on Nested Forms for help. - We should be able to select previously created tags as well as create a new tag.
- Remember, because we have a uniqueness validation on the name of tag, we will need to account for that.
- A user shouldn't have to submit a new tag every time they submit a post.
class User < ActiveRecord::Base
has_many :posts
accepts_nested_attributes_for :posts, reject_if: proc { |attributes| attributes['title'].blank? }
end
- To allow a user to create a new tag, the controller action for a new post should instantiate a new tag. Check out the documentation for the
fields_for
tag.
View Rails Blog: Complex Nested Forms on Learn.co and start learning to code for free.