- 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. While, ideally, every song is associated with an artist, this won't always be the case in our application. We'll use helpers to display different links based on whether or not a song is associated with an artist.
When a user adds a song to the library, they might be in a hurry and not know the artist off the top of their head, leaving it blank.
When we display the song library to the user, we want to account for when no artist has been associated with any given song. Additionally, we want to prompt the user to add an artist (mostly 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 for
#artist_name
and#artist_name=
so that anArtist
can be retrieved from, and associated with, aSong
instance - Write a helper method
#display_artist
in the appropriateapp/helpers
file to be called on in our views. The method's return value should take into consideration whether an artist is already associated with a song:
- If an artist is already associated with the song, return a link to the artist's
show
page - If an artist is not associated with the song (a.k.a. 'else'), return a link to the song's
edit
page, with a link text of "Add Artist"
- Use the helper to display the artist on the
songs#show
andsongs#index
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.