Refactoring Views With Helpers Lab
Objectives
Review creating associated objects
Write a helper to conditionally display different links
Use the helper in multiple views
Song Library
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).
Instructions
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_nameand#artist_name=so that anArtistcan be retrieved from, and associated with, aSonginstanceWrite a helper method
#display_artistin the appropriateapp/helpersfile 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
showpageIf an artist is not associated with the song (a.k.a. 'else'), return a link to the song's
editpage, with a link text of "Add Artist"
Use the helper to display the artist on the
songs#showandsongs#indexpagesMake sure all tests pass. Then, do this:
Clone : https://github.com/learn-co-curriculum/refactoring-views-with-helpers-lab
Last updated