Giter Site home page Giter Site logo

mottigottlieb / rails-blog-nested-forms-v-000 Goto Github PK

View Code? Open in Web Editor NEW

This project forked from learn-co-students/rails-blog-nested-forms-v-000

0.0 2.0 0.0 737 KB

License: Other

Ruby 76.51% JavaScript 1.55% CoffeeScript 0.51% CSS 4.24% HTML 17.18%

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.

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

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; 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 tag 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 roseweixel avatar annjohn avatar blake41 avatar irmiller22 avatar pletcher avatar fislabstest avatar fs-lms-test-bot avatar garettarrowood avatar peterbell avatar sophiedebenedetto avatar sarogers avatar victhevenot avatar asialindsay avatar

Watchers

James Cloos avatar Motti Gottlieb avatar

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.