Code Monkey home page Code Monkey logo

rails-blog-nested-forms-v-000's Introduction

Rails Blog: Complex Nested Forms

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.

Tags

  1. 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.
  2. We also need an accepts_nested_attributes_for macro on our Post model, which will permit tags to be nested in our new Post form.
  3. Now we can build a nested form in 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 uniqueness validation on the name of tag, we will need to account for that.
  6. 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
  1. 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.

Resources

View Rails Blog: Complex Nested Forms on Learn.co and start learning to code for free.

rails-blog-nested-forms-v-000's People

Contributors

ahimmelstoss avatar annjohn avatar asialindsay avatar blake41 avatar danielseehausen avatar drakeltheryuujin avatar fislabstest avatar fs-lms-test-bot avatar garettarrowood avatar gj avatar irmiller22 avatar johann avatar maxwellbenton avatar peterbell avatar pletcher avatar roseweixel avatar sarogers avatar sophiedebenedetto avatar victhevenot avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

rails-blog-nested-forms-v-000's Issues

Change name for title

class User < ActiveRecord::Base
has_many :posts
accepts_nested_attributes_for :posts, reject_if: proc { |attributes| attributes['title'].blank? }
end

Should have 'name' instead of 'title'

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.