Coder Social home page Coder Social logo

namespaced-routes-lab-v-000's Introduction

Namespaced Routes Lab

Objectives

  1. Organize controllers using a module.
  2. Use namespaced routes.

Overview

We're going to add some administrative functions to our song library. Using what we learned about namespaced routes and module scope, we'll organize our controllers and routes under an admin namespace to keep them separate from the regular user functions.

Instructions

The base application has been provided with tests. Make sure to run rake db:seed to set up seed data. Tests can be run with rspec.

Note: Since we're building new features on an existing project that already has tests, part of the job is to make sure the tests that already pass at the beginning still pass when you're done!

  1. Create a Preference model that will store preferences for the app. It will need to have fields for:
    • Allowing creation of new songs. Used to control the ability to add new songs to the system.
    • Allowing creation of new artists. Used to control the ability to add new artists to the system. Note: There will only be 1 instance of Preference, not a preference associated with each artist/song.
  2. Create a PreferencesController, routes, and views. Do this under an Admin module to separate it from the standard user functionality.
  3. Update the songs#new and artists#new actions to check that creating new songs or artists is enabled, and redirect to /songs and /artists, respectively, if that preference is disabled.
  4. Make sure tests pass.

Happy Gilmore

View Namespaced Routes Lab on Learn.co and start learning to code for free.

namespaced-routes-lab-v-000's People

Contributors

annjohn avatar blake41 avatar drakeltheryuujin avatar gj avatar jmburges avatar maxwellbenton avatar pletcher avatar scottcreynolds avatar sgharms avatar

Watchers

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

namespaced-routes-lab-v-000's Issues

Confusing directions

Unclear how students should be associating to the instance of Preference and where that instance should be created.

ArtistsController and SongsController redirect tests

The redirect tests passed with the following code in each controller:

def new
  redirect_to artists_path
  @artist = Artist.new
 end

 def new
   redirect_to songs_path
   @song = Song.new
 end

With that said, it is possible to pass all tests with only the following in the preferences controller.

def index

end

Rails version 5 and above - required validation with belongs_to association

In Rails version 5 and above, when using belongs_to associations, validations requiring the associated model object upon creation are turned on by default. This creates a validation error when the test is purposely creating a new Song without an associated Artist.

1) songs /songs/:id links to edit when no artist
     Failure/Error: visit song_path(song)
     
     ActionController::UrlGenerationError:
       No route matches {:action=>"show", :controller=>"songs", :id=>#<Song id: nil, title: "Policy of Truth", created_at: nil, updated_at: nil, artist_id: nil>}, possible unmatched constraints: [:id]
     # ./spec/features/songs_spec.rb:46:in &#96;block (3 levels) in <main>'

To fix this, add to Songs model: belongs_to :artist, optional: true. Alternatively, the defaults can be turned off by adding Rails.application.config.active_record.belongs_to_required_by_default = false to the end of config/application.rb file.

Songs new controller action redirect test is not bulletproof

I have my attribute default set to true in the migration.


def new
    @new_song_check = Preference.new
    # ISSUE: in Artists controller, this was wrong
    # This should be creating the new instance, not redirecting when true, right? The test says this is correct.
    if @new_song_check.allow_create_songs
      redirect_to songs_path
    else
      @song = Song.new
    end
  end

end```

LoadError: cannot load such file -- active_storage/engine

After cloning down the lab and running rake db:seed, I was given this error:

$ rake db:seed
rake aborted!
LoadError: cannot load such file -- active_storage/engine
...

The fix (most likely):

  • Delete Gemfile.lock
  • In the Gemfile, change gem 'rails', '5.0.7.1' to gem 'rails'
  • Add gem 'listen'

Readme instuctions differ from specs

In the readme, it tells us to create one controller for preferences, but the specs splits the preferences into two controllers. One for settings, and one for access.

features/songs_spec 45 artist must exist; cannot save

Received error:
`1) songs /songs/:id links to edit when no artist
Failure/Error: visit song_path(song)

 ActionController::UrlGenerationError:
   No route matches {:action=>"show", :controller=>"songs", :id=>#<Song id: nil, title: "Policy of Truth", created_at: nil, updated_at: nil, artist_id: nil>}, possible unmatched constraints: [:id]
 # ./spec/features/songs_spec.rb:46:in `block (3 levels) in <main>'`

Calling '.errors' on an instance of Song results in the following message:

song.errors => #<ActiveModel::Errors:0x007fc38ba5a950 @base=#<Song id: nil, title: "Policy of Truth", created_at: nil, updated_at: nil, artist_id: nil>, @messages={:artist=>["must exist"]}, @details={:artist=>[{:error=>:blank}]}>

Resolved by appending the following code in the Song model:
belongs_to :artist, optional: true

This relinquishes the requirement for an Artist to exist upon the creation of a Song object.

Thank you

This Lesson Needs Reworking

The tests do not have anything to do with instructions in the readme.
If you create a Preference model and save it, is that preference for a specific user? Is that preference for the whole app?
There are unit tests for the Preference model, but the controller tests don't create any preferences so there's no way to determine how to sort, and the tests don't even care about sort order anyways.

This lab should either be fixed or removed. Whoever let this into the curriculum didn't spend any amount of time reviewing it... ๐Ÿ˜ข ๐Ÿผ

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.