Code Monkey home page Code Monkey logo

rails-blog-nested-forms'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

rails-blog-nested-forms'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 ihollander avatar irmiller22 avatar johann avatar lizbur10 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  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

rails-blog-nested-forms's Issues

Issue with `post_spec.rb` test

I believe line 11 in the tests should only be tags_attributes: {} instead of tags_attributes [{}]. With the original line given in the tests, it raises an error which requires extra steps in the tags_attributes= method which you normally would not need, since Rails would pass the argument for that method as a hash rather than an array with a hash.

Gemfile: Ruby version 2.2.0 required

When I ran bundle install I got this message:

14:52:28] (master) rails-blog-nested-forms-v-000
// ♥ bundle install
Your Ruby version is 2.2.3, but your Gemfile specified 2.2.0
[14:52:53] (master) rails-blog-nested-forms-v-000
// ♥ rvm install ruby-2.2.0

After installing 2.2.0, I tried running learn and got this:

// ♥ learn
/System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/rubygems/dependency.rb:296:in `to_specs': Could not find 'learn-co' (>= 0) among 16 total gem(s) (Gem::LoadError)
    from /System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/rubygems/dependency.rb:307:in `to_spec'
    from /System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/rubygems/core_ext/kernel_gem.rb:47:in `gem'
    from /usr/local/bin/learn:22:in `<main>'

I don't know if I did something wrong, but if I just set the Gemfile to my current Ruby version:

ruby '2.2.3'

and everything seems to be running well. I checked another student (who completed the lab already) and they had changed it to 2.2.3, too.

Learn Command isn't working

I can't get my tests working. I've tried to delete the gem file.lockm reinstall bundler and reinstall learn-co.

Instructions do not match the assignment.

Hi, I just wanted to point out that the instructions for this lab only vaguely match what the student is supposed to do. There are also a lot of junk files (_form.html.erb AND form.html.erb intead of just the former).

Errors asking for a manifest.js

When this project is being opened, the tests are asking for:

Failure/Error: <%= stylesheet_link_tag    "application", media: "all", "data-turbolinks-track" => true %>
     ActionView::Template::error:
       Asset `application.css` was not declared to be precompiled in production.
       Declare links to your assets in `app/assets/config/manifest.js`.
         //= link application.css
       and restart your server

When we created the config folder and manifest.js files, these went away - should they be included in the lab already?

Points of confusion

There are a few things in the instructions for this lab that are confusing.

  1. The first part of #5 sounds like it's talking about the need to create a custom attribute writer that incorporates find_or_create_by, but that doesn't seem like it has anything to do with whether a user has to submit a new tag every time they submit a post. Should the second half of the sentence be a separate point #6?
  2. Partly because of this confusion, the code snippet is also confusing -- based on placement, it seems like it should be related to the uniqueness validation. The fact that it uses User and Post as an example -- models that are in the lab but are not where the code needs to be included -- adds to the confusion.
  3. I believe the last item (labeled 1 but really 6) should say "... the controller action for a new post ... " instead.

Also, all of the tests pass without the custom attribute writer. With the custom attribute writer, one test fails (the first test in /spec/models/post_spec.rb), as described in issue #23

blake notes to self

hidden field adds an additional empty item in the tag_ids array
tag_ids=([ids]) is the writer
tag_ids is the reader, both defined by the has_many :tags association
make sure previous lab discusses creating custom tags_attributes writer
make sure previous lab discusses tag_ids and how that works (for the collection select)
all above dealt with in https://github.com/learn-co-curriculum/has-many-through-forms-rails

fields_for needs accepts nested attributes to work
this is dealt with here https://github.com/learn-co-curriculum/basic-nested-forms/blob/master/README.md

collection_check_boxes
collection select talked about herehttps://github.com/learn-co-curriculum/forms-and-basic-associations-rails
accepts nested attributes reject to ignore when we dont want to create a new tag
discuss why there is a hidden field generated by collection_select
(i think) http://api.rubyonrails.org/classes/ActionView/Helpers/FormBuilder.html#method-i-check_box

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.