Week 08 Subscriptions - Code-the-Dream-School/rails-guidebook GitHub Wiki
| Week | Topic | Learning Objectives | Key Resources |
|---|---|---|---|
| 8 | Subscriptions |
|
Lesson Materials - See Coding Assignment Coding Assignment |
- Extend the application by implementing user subscriptions to forums.
-
Create New Branch
- Create a new branch
lesson7_2for this work.
- Create a new branch
-
Generate Subscription Scaffold
- Use the scaffold generator to create the Subscription controller and views, skipping certain steps.
-
Edit Subscriptions Controller
- Add
before_actionmethods to check user login and set the forum. - Modify
set_subscriptionfor authorization checks. - Update controller methods to handle subscription operations with proper user and forum context.
- Add
-
Update Subscription Views
- Modify
_form.html.erbto set the form action based on subscription presence. - Update
_subscription.html.erbto display forum names instead of IDs. - Adjust
index.html.erbto render forums with subscription actions and remove unnecessary links.
- Modify
-
Additional Functionality
- Add a check in the
newmethod to prevent duplicate subscriptions. - Discuss appearance, human factors, and potential improvements for a real application.
- Add a check in the
-
Testing
- Test the application to ensure all functionalities work as expected.
-
Submit Work
- Commit changes, push to the
lesson7_2branch, and create a pull request.
- Commit changes, push to the
-
Branch Creation
- Create a new branch
lesson7_2.
- Create a new branch
-
Generate Subscription Scaffold
- Run
bin/rails g scaffold subscription forum:references user:references priority:integer --skip-collision-check --skip-routes --no-migration. - Remove
resources :subscriptionsfromconfig/routes.rb.
- Run
-
Edit Subscriptions Controller
- Add
before_action :check_logon. - Add
before_action :set_forum, only: %w[new create]. - Modify
set_subscriptionto includeuser_idcheck. - Update
indexmethod to join forums and subscriptions, ordered by priority. - Modify
newmethod to set subscription with forum and user context. - Update
createmethod to handle subscription creation. - Ensure
updatemethod handles subscription updates. - Modify
destroymethod to properly destroy subscriptions.
- Add
-
Update Subscription Views
- Modify
_form.html.erbto set form action based on subscription presence. - Remove forum_id and user_id input fields from
_form.html.erb. - Add forum name display to
_form.html.erb. - Add priority range to priority input field.
- Update
_subscription.html.erbto display forum names. - Adjust
index.html.erbto render forums with subscription actions. - Remove "new subscription" link from
index.html.erb. - Add "Subscribe" link to forums index view.
- Modify
-
Additional Functionality
- Add check in
newmethod to prevent duplicate subscriptions.
- Add check in
-
Testing
- Test all functionalities to ensure proper working.
-
Submit Work
- Add, commit, and push changes for
lesson7_2branch. - Create a pull request for the changes.
- Add, commit, and push changes for