samuelvary1 / rails-blog-nested-forms-web-1115

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Rails Blog: Complex Nested Forms

We're going to build off our previous iteration of our Blog App, where we created new models for Users and Tags (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.

Before anything, note that when you generate models, controllers, etc, be sure to include this option, so that it skips tests (which we already have): --no-test-framework

Tags

  1. We need to change the permitted params in our post controller to accept another attribute: :tags_attributes, which permit the tag attributes that we need to create a new tag.
  2. accepts_nested_attributes_for on Post model, which will permit tags to be nested in our new post form.
  3. Now we can build a nested form on our Post form. Check out the documentation on Nested Forms for help.
  4. We should be able to select previously created tags, as well as create a new tag.
  5. Remember, because we have a validation on name presence 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. To do this, the form for a new tag should instantiate a new tag. Check out the documentation for the fields_for tag.

Resources

Strong Params

Nested Forms

fields_for tag

Preventing Empty Records

View this lesson on Learn.co

About

License:Other


Languages

Language:Ruby 75.8%Language:HTML 17.7%Language:CSS 4.4%Language:JavaScript 1.6%Language:CoffeeScript 0.5%