Giter VIP home page Giter VIP logo

has-many-through-forms-rails-labs-v-000'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:

    • The title, categories, and content of the post.
    • All of the comments associated with the post.
    • 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.
    • 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. To associate a new comment with a post, you'll have to use a hidden field:
<%= f.hidden_field :post_id, value: @post.id %>
  1. Create a users#show page that links to all of the posts a user has commented on.

  2. 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-v-000's People

Contributors

annjohn avatar aviflombaum avatar blake41 avatar danielseehausen avatar drakeltheryuujin avatar franknowinski avatar ipc103 avatar maxwellbenton avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

has-many-through-forms-rails-labs-v-000's Issues

No soultion

There are no solutions for this lab. This was a tough one for me, would love to compare my results.

Unclear tests for Post NEW section

In the current test suite, there are 4 tests under the Post NEW section:

Posts
  new
    can create a post without a new category
    can create a post with a previously created category
    can create a post with a brand new category
    can create a post with a brand new category and an existing category

I was stuck on tests 2,3,4 for the longest time, and couldn't figure out why my tests were failing.

After 30mins and looking at another student's solution - I realized these 3 tests were checking for categories to be displayed on the posts/show.html.erb...

In the original instructions, the section on Post SHOW page states:

Create a posts#show page to display the following:

  1. The title and content of the post.
  2. All of the comments associated with the post.
  3. 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.
  4. 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.

The instructions did not ask for categories to be displayed on the #show page, so it was very confusing that tests in the posts#new section was dependent on the #show page...!

Please update the instructions by:

  • Changing the test to check for @post.categories_id directly
  • Move the 3 confusing tests to post#show section
  • Include categories for post#show page instructions

Thanks,
Lisa

Unclear how to associate post with new comment

Hi,

I think it would be helpful to have a little more direction as to how to associate a new comment in the show page with that post. There are no instructions in any of the read mes that you have to use a hidden field in your comment form like so: <%= f.hidden_field :post_id, value: @post.id %>

Thanks,

Madeline

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.