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_2
for 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_action
methods to check user login and set the forum. - Modify
set_subscription
for authorization checks. - Update controller methods to handle subscription operations with proper user and forum context.
- Add
-
Update Subscription Views
- Modify
_form.html.erb
to set the form action based on subscription presence. - Update
_subscription.html.erb
to display forum names instead of IDs. - Adjust
index.html.erb
to render forums with subscription actions and remove unnecessary links.
- Modify
-
Additional Functionality
- Add a check in the
new
method 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_2
branch, 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 :subscriptions
fromconfig/routes.rb
.
- Run
-
Edit Subscriptions Controller
- Add
before_action :check_logon
. - Add
before_action :set_forum, only: %w[new create]
. - Modify
set_subscription
to includeuser_id
check. - Update
index
method to join forums and subscriptions, ordered by priority. - Modify
new
method to set subscription with forum and user context. - Update
create
method to handle subscription creation. - Ensure
update
method handles subscription updates. - Modify
destroy
method to properly destroy subscriptions.
- Add
-
Update Subscription Views
- Modify
_form.html.erb
to 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.erb
to display forum names. - Adjust
index.html.erb
to 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
new
method to prevent duplicate subscriptions.
- Add check in
-
Testing
- Test all functionalities to ensure proper working.
-
Submit Work
- Add, commit, and push changes for
lesson7_2
branch. - Create a pull request for the changes.
- Add, commit, and push changes for