- Practice defining associations
- Practice building forms in ERB when working with nested models
In this lab, we're going to make a song library that helps record thoughts
about various Song
s. Our data model looks like this:
Artist
- has a
name
attribute (String
) - has many
Song
s
- has a
Song
- has a
title
attribute (String
) - belongs to an
Artist
- belongs to a
Genre
- has many
Note
s
- has a
Genre
- has a
name
attribute (String
) - has many
songs
- has a
Note
- has
content
attribute (String
) - belongs to a
Song
- has
- The base models, controllers, and seed data have been provided for you.
- You should create and migrate the database before starting to develop your solution.
- Seeding the database provides many
Genre
s. You will add data aboutArtist
s,Note
s, andSong
s during the development of this application. TheArtistsController
andSongsController
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 aSong
and anArtist
's name. To help make your form work easier, solve thespec/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 forartist_name
andartist_name=
, as well asnote_contents
andnote_contents=
. - The
artist_name
getter method should return the name of theArtist
associated with theSong
as a string. - The
artist_name=
setter method should take a string for an artist's name, and create a newArtist
with that name associated with theSong
. - The
note_contents
getter method should return an array of strings for all theNote
contents associated with theSong
. - The
note_contents=
setter method should take an array of string representingNote
contents, and create a newNote
for each element in the array.
- This test requires that you create custom getter and setter methods in the
- 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 theSongsController
.