Giter Site home page Giter Site logo

has-many-through-forms-rails-labs-nyc-web-080618's Introduction

Has Many Through Forms Rails Labs

Overview

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.

Objectives

  1. Construct a bi-directional has many through.
  2. Identify the join model in a has many through.
  3. Construct a nested params hash with data about the primary object and a has many through association.
  4. Use the conventional key names for associated data (association_attributes).
  5. Name form inputs correctly to create a nested params hash with has many through association data.
  6. Define a conventional association writer for the primary model to properly instantiate associations based on the nested params association data.
  7. 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.
  8. Use fields_for to generate the association fields.

Instructions

  1. 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 a Category 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.

  2. Create a posts#show page to display the following:

  3. The title and content of the post.

  4. All of the comments associated with the post.

  5. 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.

  6. 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.

  7. Create a users#show page that links to all of the posts a user has commented on.

  8. Create a categories#show page that links to all of the posts associated with a given category.

Hints

  • When creating new comments, we should only create a new user if they filled in that input. accepts_nested_attributes_for has a reject_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 for collection_select.

Resources

View Has Many Through Forms Rails Labs on Learn.co and start learning to code for free.

has-many-through-forms-rails-labs-nyc-web-080618's People

Contributors

ipc103 avatar annjohn avatar danielseehausen avatar aviflombaum avatar blake41 avatar franknowinski avatar

Watchers

 avatar James Cloos avatar  avatar Victoria Thevenot avatar  avatar Joe Cardarelli avatar Sam Birk avatar Sara Tibbetts avatar The Learn Team avatar Sophie DeBenedetto avatar  avatar Antoin avatar Alex Griffith avatar  avatar Amanda D'Avria avatar  avatar A. Perez avatar Nicole Kroese  avatar Lore Dirick avatar Nicolas Marcora avatar Lisa Jiang avatar  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.