- Review creating associated objects
- Write a helper to conditionally display different links
- Use the helper in multiple views
In this lab, we're going to augment our song library to show the artists associated with songs, and use helpers to display different links based on whether or not a song is associated with an artist.
When we add songs to our library, we want to be sure to include the artist so that we can keep things organized. Sometimes, however, we might be in a hurry and don't know the artist off the top of our heads, so we leave it blank.
When we display the song library, we want to include the artist, but if there's no artist associated, we want to prompt the user to add one, because we don't want to contribute to the global iTunes library "Unknown Artist" epidemic.
The base models, controllers, views and other files have been provided. There are tests for the lab in the spec
directory. You can run tests with the rspec
command.
- Write the code to get (
artist_name
) and set (artist_name=
) theArtist
associated with aSong
model. - Write a helper method to display the artist for a song, with the following considerations:
- Put the method in the appropriate helper file following the principle of Separation of Concerns
- Name the method
display_artist
- If the artist name is not
nil
, return a link to the artist'sshow
page. - If the artist name is
nil
, return a link to the song'sedit
page, with a link text of "Add Artist"
- Use the helper to display the artist on the song
show
andindex
pages. - Make sure all tests pass then do this:
View Refactoring Views With Helpers Lab on Learn.co and start learning to code for free.