Giter VIP home page Giter VIP logo

forms-and-basic-associations-rails-lab's Introduction

Forms And Basic Associations Rails Lab

Objectives

  1. Practice defining associations
  2. Practice building forms in ERB when working with nested models

A Song Library

In this lab, we're going to make a song library that helps record thoughts about various Songs. Our data model looks like this:

  • Artist
    • has a name attribute (String)
    • has many Songs
  • Song
    • has a title attribute (String)
    • belongs to an Artist
    • belongs to a Genre
    • has many Notes
  • Genre
    • has a name attribute (String)
    • has many songs
  • Note
    • has content attribute (String)
    • belongs to a Song

Instructions

  1. The base models, controllers, and seed data have been provided for you.
  2. You should create and migrate the database before starting to develop your solution.
  3. Seeding the database provides many Genres. You will add data about Artists, Notes, and Songs during the development of this application. The ArtistsController and SongsController have been built out so that you can do this.

First, connect the models by using the ActiveRecord association commands.

Next, update the minimal app/views/songs/new.html.erb.

This view should have a form that provides:

  • A text input box that sets the Song's title.
  • A text input box for the Artist.
  • A selection box for Genre. Users should be able to pick amongst existing genres only.
  • Several text input boxes to add notes to the song. These should have the IDs song_notes_1, song_notes_2, and so on for the specs to pass.

This is a challenging lab. Here are some hints:

  • You might need to search around for how to pass an array using strong_params!
  • It's easy to get confused between getting an Artist instance from a Song and an Artist's name. To help make your form work easier, solve the spec/models/song_spec.rb first. You can run a single spec by invoking it with e.g. rspec spec/models/song_spec.rb.
    • This test requires that you create custom getter and setter methods in the Song class for artist_name and artist_name=, as well as note_contents and note_contents=.
    • The artist_name getter method should return the name of the Artist associated with the Song as a string.
    • The artist_name= setter method should take a string for an artist's name, and create a new Artist with that name associated with the Song.
    • The note_contents getter method should return an array of strings for all the Note contents associated with the Song.
    • The note_contents= setter method should take an array of string representing Note contents, and create a new Note for each element in the array.
  • Make use of the references below!
  • While we direct you to update new.html.erb, you're going to need to make changes in multiple models and the SongsController.

References

forms-and-basic-associations-rails-lab's People

Contributors

annjohn avatar aviflombaum avatar danielseehausen avatar drakeltheryuujin avatar genericlady avatar ihollander avatar ipc103 avatar jmburges avatar lkwlala avatar maxwellbenton avatar mendelb avatar pletcher avatar queerviolet avatar rrcobb avatar sdcrouse avatar sgharms avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

forms-and-basic-associations-rails-lab's Issues

Dead code in solution branch?

In the solution branch, the SongsController has p song_params in the create method. What is that? Why is it there?

I had a student ask me and I had no reasoning to give him.

note_contents=

On the solution, I noticed 2 odd things about the note_contents= implementation. I'm not sure if these are intentional.

  1. Say you have an existing song s with note_contents ["note 1", "note 2"]. If you call s.note_contents=["Revised Notes"], it looks like the note contents are now "note 1", "note 2" and "Revised Notes". I think I would have expected the old notes to be replaced by the new assignment.

  2. Lets say there is a song s1 with note "Recorded July 2018" and another song s2 with note "Recorded July 2018". It looks like both these songs will be pointing to the same note id. It seems that this could lead to problems (like if s1 is deleted including references to notes, then s2 will be missing its note?)

Was this behavior intended? I tried to address 2nd item in my implementation but I think a little messy
def note_contents=(arry)
arry.delete_if(&:blank?).each { |c|
if !Note.find_by(content: c, song_id: self.id)
self.notes.build(content: c)
end
}
end

form_spec and model spec require two different genre setters to pass?

In song_form_spec.rb, the test to create a song with a genre is looking for a song_genre_id field

select 'Alternative', from: 'song_genre_id'

which I understand will mean that a genre_id=(id) setter will be needed in the Song model. This will also mean that the strong params will need to permit :genre_id. However, in song_spec.rb, it tests for a genre_name setter and getter method, as well.

song.genre_name = 'Rock'

Working through the specs, I was struggling to get all the tests passing. It was a bit confusing having to write both genre setter/getter methods into the Song model to get the tests to pass. I also noticed that the genre_id setter/getter was not included into the solution, and I was not able to pass the lab without those methods. As I was looking through the history of the solution branch, I noticed that the New form did use :genre_name at one point before being switched over to :genre_id.

Feedback from Student

In the Rails Forms and Basic Associations Lab, in the songs_controller, how are we supposed to know the format of note_contents in the strong params? (note_contents: [ ]) A future lab (Has Many Through In Forms) goes over this.

Tests and solution no longer fit the original intent of the lab

I worked with a student on this today and it seems that sometime in the recent past the tests and the solution branch have been refactored to use the accepts_nested_attributes rails helper which hasn't yet been taught to the students. There should be no expectation for them to be using this method to complete the lab. The old solution and tests tested for the manual creation of the notes via a notes_contents=method as well as manually creating the html input elements on the page.

Here is my fork of the lab which better follows the intent of the lab in context with the previous readme lesson: https://github.com/Sillhouette/forms-and-basic-associations-rails-lab-v-000

@rrcobb @maxwellbenton

solution has additional specs

The solution branch has some controller tests and model tests for song that aren't included on the master branch. I'm guessing these should be added into the master branch.

Also, there are several pending specs in both master and solution that should be removed.

Lesson did not cover this lab

Confusing lab. The previous lesson did not do a good job of explaining the data flow in this lab as well as the methods. The method below was not discussed at all in the previous lab and it's not intuitive.

    notes.each do |content|
      if content.strip != ''
        self.notes.build(content: content)
      end
    end
  end

  def note_contents
    self.notes.map(&:content)
  end 

No test for artist name autocompletion

https://github.com/learn-co-curriculum/forms-and-basic-associations-rails-lab#instructions

Instructions

The base models, controllers, and seed data have been provided for you. The associations have not been wired up.

  • Write app/views/songs/new.html.erb. This form should have:
    • A text input box that sets the song's title.
    • A text input box for the artist, which autocompletes with existing Artist names.

There's currently no test to ensure that the artist text input box "autocompletes with existing Artist names." Also, the solution branch code does not implement this feature.

Songs have many notes

Under instructions, it says: "Songs belong to both and have many notes." So I think "has many notes" should also be added to Song bullet points.

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.