Week 09 Rails Basic Skills - Code-the-Dream-School/rails-guidebook GitHub Wiki
Week | Topic | Learning Objectives | Key Resources |
---|---|---|---|
9 | Rails Basic Skills |
|
Lesson Materials Coding Assignment |
- Understand the Rails MVC architecture, routing, debugging, and model validation through a practical exercise.
-
Rails MVC Overview
- Explanation of Model, View, Controller (MVC) architecture in Rails.
- Details on how Rails handles web requests and routing.
-
Convention over Configuration
- Introduction to Rails' opinionated framework and default behaviors.
-
Saving State in Rails
- Explanation of how to store state data in Rails applications.
-
Rails Request Handling
- Explanation of HTTP verbs, routing, and controller methods in Rails.
-
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.
-
Rails Debugging
- Introduction to Ruby's built-in debugger and how to use it in Rails.
- Debugging practice with a sample error.
-
Exception Handling
- Adding exception handling in the CustomersController.
- Displaying user-friendly error messages using flash alerts.
-
Model Validations
- Adding and testing validations for the Customer model.
- Introduction to the email-validator gem for email validation.
-
Error Handling in Controllers
- Implementing error handling in the create and update methods of the CustomersController.
- Displaying validation errors to users.
-
Additional Model Methods
- Adding a custom method in the Customer model and using it in the views.
-
Submitting Work
- Instructions on committing, pushing changes, and creating pull requests.
-
Rails MVC Overview
- Review the explanation of the Model, View, Controller (MVC) architecture.
- Understand how Rails handles web requests and routing.
-
Convention over Configuration
- Review Rails' opinionated framework and default behaviors.
-
Saving State in Rails
- Understand how to store state data in Rails applications.
-
Rails Request Handling
- Understand HTTP verbs, routing, and controller methods in Rails.
-
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'
.
-
Rails Debugging
- Add
debugger
statement incustomers_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.
- Add
-
Exception Handling
- Add
rescue_from ActiveRecord::RecordNotFound, with: :catch_not_found
tocustomers_controller.rb
. - Implement
catch_not_found
method to handle exceptions. - Test exception handling by accessing a non-existent customer.
- Add
-
Model Validations
- Add validations to
Customer
model. - Add
gem 'email_validator'
to Gemfile and runbundle install
. - Test validations by creating and updating customers with invalid data.
- Add validations to
-
Error Handling in Controllers
- Update
create
method incustomers_controller.rb
with error handling. - Update
update
method incustomers_controller.rb
with error handling. - Test error handling by creating and updating customers with invalid data.
- Update
-
Additional Model Methods
- Add
full_name
method toCustomer
model. - Update views to use
full_name
method. - Verify changes in the browser.
- Add
-
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.
- Commit and push changes to