Skip to content

powercodeacademy/forms-and-basic-associations-rails-lab

 
 

Repository files navigation

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 (make this optional with the optional: true option)
    • belongs to a Genre (make this optional with the optional: true option)
    • 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.

Hints:

  • You might need to search 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 bin/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 you are directed to update new.html.erb, you will need to make changes in multiple models and the SongsController. 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

About

No description, website, or topics provided.

Resources

License

Contributing

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Ruby 82.2%
  • HTML 14.6%
  • CSS 1.6%
  • JavaScript 1.6%