- Organize controllers with a module
- Use namespaced routes
We're going to add some administrator functions to our song library and use what we learned about namespaced routes and module scope to organize our routes and controllers.
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!
- Create a model called
Preference
with twostring
fields:song_sort_order
andartist_sort_order
and twoboolean
fields:allow_create_artists
andallow_create_songs
. - Implement a
SettingsController
under anAdmin
module withindex
andupdate
actions. It will be responsible for maintaining twopreference
entries namedsong_sort_order
andartist_sort_order
. There should only be one record for each so tryfirst_or_create
to create an@preference
in the controller. The defaultvalue
for each sort preference should be "DESC". Redirect tosettings#index
after update.
- Hint: Because Rails doesn't know how to "count"
Settings
, the helper for the index route will be different than you're used to. Instead ofadmin_settings_path
going to/admin/settings
, you'll need to useadmin_settings_index_path
. Make sure to runrake routes
to verify your URL helper names.
- Implement routes and views for the
SettingsController
. Use radio buttons for the values. Acceptable values are "ASC" and "DESC". Both preferences should be editable from theindex
view whether new or existing. - Update the
artists#index
action to order byname
withartist_sort_order
and thesongs#index
action to order songs bytitle
withsong_sort_order
. Make sure to check that the value exists and set it to the default if it doesn't. - Implement an
AccessController
under theAdmin
module withindex
andupdate
actions. It will be responsible for maintaining two preference entries namedallow_create_artists
andallow_create_songs
. As with theSettingsController
, usefirst_or_create
to ensure we only have onepreference
. The default values forallow_create_songs
andallow_create_artists
should betrue
. Redirect toaccess#index
after update. - Implement routes and views to manipulate for the
AccessController
. Use radio buttons for the values. Acceptable values are "true" and "false". Follow the same guidelines as forSettingsController
views in step 3.
- Note: We will have the same URL helper change here as we do for
SettingsController
, so check yourrake routes
to ensure you use the right helpers.
- Update the
songs#new
andartists#new
actions to check the access control preferences and redirect to/songs
and/artists
, respectively, if creation is disabled. - Make sure tests pass.
- Ride the bull. Feel the flow.
View Objectives on Learn.co and start learning to code for free.