Week 09 Rails Basic Skills - Code-the-Dream-School/rails-guidebook GitHub Wiki

Week Topic Learning Objectives Key Resources
9 Rails Basic Skills
  • Understanding the Model/View/Controller Approach for aeb application frameworks
  • Understand the flow of a web request coming Into Rails
  • Acquire basic skills with the Ruby debugger in Rails
  • Learn how to handle exceptions that may be thrown by Rails functions
  • Learn how to vaildate user input
  • Handle user input errors
Lesson Materials

Coding Assignment

Lesson 8: High-Level Outline

Objective

  • Understand the Rails MVC architecture, routing, debugging, and model validation through a practical exercise.

Steps Covered

  1. Rails MVC Overview

    • Explanation of Model, View, Controller (MVC) architecture in Rails.
    • Details on how Rails handles web requests and routing.
  2. Convention over Configuration

    • Introduction to Rails' opinionated framework and default behaviors.
  3. Saving State in Rails

    • Explanation of how to store state data in Rails applications.
  4. Rails Request Handling

    • Explanation of HTTP verbs, routing, and controller methods in Rails.
  5. Getting Started with a Customer App

    • Fork and clone the repository, install gems, and set up a new Rails application.
    • Generate scaffold for Customer model and set the root path.
  6. Rails Debugging

    • Introduction to Ruby's built-in debugger and how to use it in Rails.
    • Debugging practice with a sample error.
  7. Exception Handling

    • Adding exception handling in the CustomersController.
    • Displaying user-friendly error messages using flash alerts.
  8. Model Validations

    • Adding and testing validations for the Customer model.
    • Introduction to the email-validator gem for email validation.
  9. Error Handling in Controllers

    • Implementing error handling in the create and update methods of the CustomersController.
    • Displaying validation errors to users.
  10. Additional Model Methods

    • Adding a custom method in the Customer model and using it in the views.
  11. Submitting Work

    • Instructions on committing, pushing changes, and creating pull requests.

Lesson 8: Checklist

  1. Rails MVC Overview

    • Review the explanation of the Model, View, Controller (MVC) architecture.
    • Understand how Rails handles web requests and routing.
  2. Convention over Configuration

    • Review Rails' opinionated framework and default behaviors.
  3. Saving State in Rails

    • Understand how to store state data in Rails applications.
  4. Rails Request Handling

    • Understand HTTP verbs, routing, and controller methods in Rails.
  5. Getting Started with a Customer App

    • Fork and clone the repository.
    • Run bin/bundle install to install gems.
    • Generate scaffold for Customer model: bin/rails generate scaffold Customer first_name:string last_name:string phone:string email:string.
    • Run bin/rails db:migrate.
    • Edit config/routes.rb to set root path: root to: 'customers#index'.
  6. Rails Debugging

    • Add debugger statement in customers_controller.rb before the failing line.
    • Re-run the server and duplicate the error to practice debugging.
    • Review and use debugger commands in the server console.
  7. Exception Handling

    • Add rescue_from ActiveRecord::RecordNotFound, with: :catch_not_found to customers_controller.rb.
    • Implement catch_not_found method to handle exceptions.
    • Test exception handling by accessing a non-existent customer.
  8. Model Validations

    • Add validations to Customer model.
    • Add gem 'email_validator' to Gemfile and run bundle install.
    • Test validations by creating and updating customers with invalid data.
  9. Error Handling in Controllers

    • Update create method in customers_controller.rb with error handling.
    • Update update method in customers_controller.rb with error handling.
    • Test error handling by creating and updating customers with invalid data.
  10. Additional Model Methods

    • Add full_name method to Customer model.
    • Update views to use full_name method.
    • Verify changes in the browser.
  11. Submitting Work

    • Commit and push changes to lesson8 branch.
    • Create a pull request for the changes.
    • Ensure all debugger statements are removed before pushing code.
⚠️ **GitHub.com Fallback** ⚠️