Week 08 Subscriptions - Code-the-Dream-School/rails-guidebook GitHub Wiki

Week Topic Learning Objectives Key Resources
8 Subscriptions
  • More practice on building Rails application elements
  • Introduction to Rails default behavior and overrides
  • Completion of the application started in Lesson 7
Lesson Materials - See Coding Assignment

Coding Assignment

Overview

Objective

  • Extend the application by implementing user subscriptions to forums.

Steps Covered

  1. Create New Branch

    • Create a new branch lesson7_2 for this work.
  2. Generate Subscription Scaffold

    • Use the scaffold generator to create the Subscription controller and views, skipping certain steps.
  3. 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.
  4. 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.
  5. Additional Functionality

    • Add a check in the new method to prevent duplicate subscriptions.
    • Discuss appearance, human factors, and potential improvements for a real application.
  6. Testing

    • Test the application to ensure all functionalities work as expected.
  7. Submit Work

    • Commit changes, push to the lesson7_2 branch, and create a pull request.

Assignment Rubric

  1. Branch Creation

    • Create a new branch lesson7_2.
  2. 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 from config/routes.rb.
  3. Edit Subscriptions Controller

    • Add before_action :check_logon.
    • Add before_action :set_forum, only: %w[new create].
    • Modify set_subscription to include user_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.
  4. 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.
  5. Additional Functionality

    • Add check in new method to prevent duplicate subscriptions.
  6. Testing

    • Test all functionalities to ensure proper working.
  7. Submit Work

    • Add, commit, and push changes for lesson7_2 branch.
    • Create a pull request for the changes.
⚠️ **GitHub.com Fallback** ⚠️