Week 06 Rails Introduction - Code-the-Dream-School/rails-guidebook GitHub Wiki
Week | Topic | Learning Objectives | Key Resources |
---|---|---|---|
6 | Rails Introduction |
|
Lesson Materials - See Coding Assignment Coding Assignment |
-
Introduction to Rails Application
- Create a Rails application from scratch.
- Explanation of the Rails server and handling HTTP requests.
-
Setup
- Fork and clone the repository.
- Create a new branch.
- Install necessary gems with
bin/bundle install
.
-
Starting the Rails Server
- Start and stop the server using
bin/rails server
.
- Start and stop the server using
-
Generating a Basic Application Using Scaffolding
- Generate a scaffold for
forum
withforum_name
. - Overview of generated components: migration, model, route, controller, views.
- Generate a scaffold for
-
Running Migrations
- Run
bin/rails db:migrate
. - Explanation of
db/schema.rb
anddb/development.sqlite3
.
- Run
-
MVC Overview
- Description of Model-View-Controller structure.
-
Fixing the Schema
- Adding
description
toforum
. - Running migration for schema changes.
- Using Rails console to add entries.
- Adding
-
Updating Views
- Modify
index.html.erb
and_forum.html.erb
to includedescription
. - Update
_form.html.erb
to include input fordescription
.
- Modify
-
Debugging and Error Handling
- Experimenting with routes and controller methods.
- Understanding and fixing common errors.
-
User Model
- Generate a scaffold for
user
. - Validate
skill_level
and update form for radio buttons. - Experiment with routes and controller methods for users.
- Generate a scaffold for
-
Simulating Logon
- Adding
logon
andlogoff
methods to the user controller. - Update routes and views for logon and logoff functionality.
- Adding
-
Setup
- Fork the repository.
- Clone the forked repository.
- Create a new branch called
lesson6
. - Run
bin/bundle install
.
-
Starting the Server
- Start the server with
bin/rails server
. - Stop the server with Ctrl-C.
- Start the server with
-
Generate Scaffold
- Generate scaffold for
forum
withforum_name:string
. - Check the generated files: migration, model, route, controller, views.
- Generate scaffold for
-
Run Migrations
- Run
bin/rails db:migrate
. - Verify creation of
db/schema.rb
anddb/development.sqlite3
.
- Run
-
Add Description to Forum
- Generate migration to add
description:string
toforums
. - Run
bin/rails db:migrate
. - Verify `
- Generate migration to add
- When you generate a scaffold, it creates:
- A model file
- A controller file with CRUD actions
- Views for each CRUD action
- A migration file
-
In config/routes.rb, you must include the HTTP method and the path. You can specify a variable using a symbol, such as :id. This creates a params[:id] variable accessible in the controller.
-
The rails db:migrate command runs the migration files, applying changes to the database schema, such as creating or altering tables.
-
The current database schema can be seen in the db/schema.rb file.
-
To show the routes, use the command rails routes.
-
Start the Rails console with rails console. You can perform CRUD operations using Active Record methods, e.g., Model.create, Model.find, Model.update, Model.destroy.
-
ERB files contain embedded Ruby code, marked by <% ... %> for code execution and <%= ... %> for code output. These statements are executed on the server side before rendering the HTML.
-
Forum is capitalized because it refers to the class name. @forum is an instance variable and follows Ruby's naming conventions. The @ symbol denotes an instance variable, making it accessible across instance methods.
-
Forum.new initializes a new instance of Forum without saving it to the database. Forum.create initializes and saves the instance in one step. Forum.save cannot be used directly because save is an instance method, not a class method.
-
The default behavior of the show action renders the show.html.erb view for the Forum instance. It knows which forum to show because of the params[:id] value, which is passed from the URL (e.g., /forums/1 sets params[:id] to 1). This value is retrieved from the route definition.