Implement the Lesson Model in the Learn to Code Rails App

Notes


We now have Course objects that have many Unit objects. The next thing we want to do is a create a Lesson object that will belong to a Unit object and a Unit object should be bale to have many Lesson objects.

Just like with the Course and Unit objects we will be using the rails g scaffold command in our terminal so that Rails will generate a lot of the boilerplate code for us. The caveat this time around is there will be a few addition attributes we want to give to the Lesson object when we generate the scaffold. Your command should look something like this:

rails g scaffold Lesson title:string content:text video_html:text unit:references

Remember, the first three attribute definitions are the attribute name (title, content, video_html) and the word following the semicolon (string, text) is the type of data expected for that attribute. The last attribute definition tells Rails that the Lesson object will belong to a Unit object, or reference it. The video_html attribute will give us the ability to use a third-party video hosting service and save the HTML they give us to play our video in the Lesson object's show page.

Anytime we create a new model, which the rails g scaffold command did for us, we need to migrate the database so let's do that now.

rake db:migrate

Just like in previous lessons with Course and Unit objects, we need to move most of the CRUD for the Lesson object behind an admin wall. Copy the Lesson views folder in app/views/admin and copy the LessonsController into the app/controllers/admin directory.

In the pasted controller, remember to prefix our controller name with the Admin module and declare the controller should use the admin layout. Also, remember to call the before_actions that will create the gateway for admin users.

The next thing we want to do is run through the base LessonsController views and delete all the links to the CRUD functionality we placed in the Admin::LessonsController. Likewise, we need to run through all the views tied to our Admin::LessonsController and point all the links for CRUD functionality at the admin version of the path eg.

<%= link_to 'Show', admin_lessons_path %>

Let's not forget to point the Lessons object form at the Admin::LessonsController and also let's copy the styling for the cards into the lesson's show page as well. Also in the lesson's show page, let's switch the link returning us back to the lesson's index page to link back to the course that it belongs to show page and display the title for Lesson's Unit instead of display the Unit object itself.

This lesson had a pretty long video but it was really just a rehash of something we've already done twice. Hopefully it was able to reinforce some of the lessons we learned earlier.

Happy coding!!!!