Giter VIP home page Giter VIP logo

refactoring-views-with-helpers-lab's Introduction

Refactoring Views With Helpers Lab

Objectives

  1. Review creating associated objects
  2. Write a helper to conditionally display different links
  3. 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.

  1. Write the code for #artist_name and #artist_name= so that an Artist can be retrieved from, and associated with, a Song instance
  2. Write a helper method #display_artist in the appropriate app/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"
  1. Use the helper to display the artist on the songs#show and songs#index pages
  2. Make sure all tests pass. Then, do this:

Dancing Daft Punk

refactoring-views-with-helpers-lab's People

Contributors

annjohn avatar bhollan avatar blake41 avatar brennenawana avatar danielseehausen avatar dependabot[bot] avatar ihollander avatar jmburges avatar lizbur10 avatar maxwellbenton avatar pletcher avatar rrcobb avatar scottcreynolds avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

refactoring-views-with-helpers-lab's Issues

Wrong method name in Song#edit

The h1 at the top uses "@song.name", but songs aren't set up that way in the db. Should be "@song.title". It doesn't affect the tests, so it's not noticeable unless you actually try to visit the edit path through a browser.

Solution Helper Method Written Incorrectly - Error during Server Usage

With the current solution code, when you create a new song without inserting an Artist name in the form, the Song is persisted to the database with a new artist that is a blank string "". The solution code for the Artist helper method (below), only checks if the song.artist is valid, not if it is a blank string.
module ArtistsHelper def display_artist(song) if song.artist link_to song.artist.name, song.artist else link_to 'Add Artist', edit_song_path(song) end end end

This leads to user errors when testing in the server. After creating this new song without an artist, the next page (song show) will not display the expected "Add Artist" link because the instance of the song does have an artist attribute of "" . This also leads to similar errors in the index page.

I believe that the solution helper method should be changed to the following to accommodate the fix.

module ArtistsHelper
def display_artist(song)
#binding.pry
if song.artist && song.artist.name != ""#is valid
link_to song.artist.name, artist_path(song.artist)
else #if nil
link_to "Add Artist", edit_song_path(song)
end
end
end

Requires Gemfile Update for Ruby 2.6.1

Ruby 2.6.1 creates errors when running with Rails 4.2.x.
Students using 2.6.1 are getting errors when running learn.

Solution: upgrade Gemfile to a rails version > 5.0, ie
gem 'rails', '5.0.7.1'

Note that bundler has to be > 2.0.x with rails > 5.0

Edit: Removing the version entirely should work as well

No Build

This lab takes so long to build that it times out and disconnects from the remote server.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.