Add Unit Views to the Admin Section of Learn to Code Rails App


The beginning of this lesson is another refresher. We're going to go through the same process of putting the Unit object's CRUD functionality behind an admin wall.

Just like the Course set up we had going on in earlier lessons, we want to copy the units folder in the app/views directory and paste that in the app/views/admin directory.

Also, copy the app/controllers/units_controller.rb file and paste it in the app/controllers/admin directory. Remember to prefix the class name to let Rails know it's in the admin module and call the authenticate_user! and ensure_admin methods in a before_action.

class Admin::UnitsController < ApplicationController
   before_action :authenticate_user!
   before_action :ensure_admin

   ...ruby code here...

Delete everything but the index, show and set_unit methods out of the base UnitsController. Also, we can delete any views that we no longer have index actions for. In our case we can delete we can just in the app/views/units directory and delete the _form.html.erb, edit.html.erb and new.html.erb files. Afterward we need to delete any links to the views or controller actions we just scrapped in the index.html.erb and show.html.erb files.

Next we're going to hop over to our application's root/config/routes.rb file and only resource Unit object paths in the admin module's namespace . Also, just like Course objects, we need to define paths for the base UnitsController since we're no longer using the resources method on it.

routes.rb after changing Unit object paths

The next thing we want to do is just clean up the form partial a little bit. Feel free to copy some code from our Course object's form because they have the same fields. One thing that was of particular importance was the need to point the form at the Admin::CoursesController and we need to do the same with the Unit object's form.

Pointing our form at the admin units controller

Now that things are cleaned up quite a bit let's go ahead and set up some links to work with our new admin Unit object routes. Let's hop over to our app/views/layouts/admin.html.erb file and create a new li tag to hold the link to our Unit object's index. Use a link_to helper method pointing at the admin_units_path and we will be able to access unit objects.

Adding a link to the admin unit index path

Just a minute ago we went through the views tied to our base UnitsController and adjusted the links to fit our needs. Now we need to do the same with the Unit views tied to Admin::UnitsController. We want full CRUD functionality for our admin users and scaffold built that for us by default. All we need to do is work through each link and make sure it's pointing at the admin namespaced controller.

<%= link_to 'Units', admin_units_path %>

One thing we forget to do with our Admin::UnitsController earlier was specifying that it should use the admin layout. Let's hop back in that file and just add

layout 'admin'

underneath our before_action calls.

Happy coding!!!!