Week 06 Rails Introduction - Code-the-Dream-School/rails-guidebook GitHub Wiki

Week Topic Learning Objectives Key Resources
6 Rails Introduction
  • Build and test a simple Rails application using a scaffold
  • Understand the elements of the application
  • Routes
  • Models, migrations, and database tables
  • Learn what Active Record does
  • Controllers
  • Views and partials
  • Understand and use embedded Ruby in template files
  • Recognise/correct various Rails error conditions
Lesson Materials - See Coding Assignment

Coding Assignment

Overview

  1. Introduction to Rails Application

    • Create a Rails application from scratch.
    • Explanation of the Rails server and handling HTTP requests.
  2. Setup

    • Fork and clone the repository.
    • Create a new branch.
    • Install necessary gems with bin/bundle install.
  3. Starting the Rails Server

    • Start and stop the server using bin/rails server.
  4. Generating a Basic Application Using Scaffolding

    • Generate a scaffold for forum with forum_name.
    • Overview of generated components: migration, model, route, controller, views.
  5. Running Migrations

    • Run bin/rails db:migrate.
    • Explanation of db/schema.rb and db/development.sqlite3.
  6. MVC Overview

    • Description of Model-View-Controller structure.
  7. Fixing the Schema

    • Adding description to forum.
    • Running migration for schema changes.
    • Using Rails console to add entries.
  8. Updating Views

    • Modify index.html.erb and _forum.html.erb to include description.
    • Update _form.html.erb to include input for description.
  9. Debugging and Error Handling

    • Experimenting with routes and controller methods.
    • Understanding and fixing common errors.
  10. User Model

    • Generate a scaffold for user.
    • Validate skill_level and update form for radio buttons.
    • Experiment with routes and controller methods for users.
  11. Simulating Logon

    • Adding logon and logoff methods to the user controller.
    • Update routes and views for logon and logoff functionality.

Assignment Checklist

  1. Setup

    • Fork the repository.
    • Clone the forked repository.
    • Create a new branch called lesson6.
    • Run bin/bundle install.
  2. Starting the Server

    • Start the server with bin/rails server.
    • Stop the server with Ctrl-C.
  3. Generate Scaffold

    • Generate scaffold for forum with forum_name:string.
    • Check the generated files: migration, model, route, controller, views.
  4. Run Migrations

    • Run bin/rails db:migrate.
    • Verify creation of db/schema.rb and db/development.sqlite3.
  5. Add Description to Forum

    • Generate migration to add description:string to forums.
    • Run bin/rails db:migrate.
    • Verify `

Lesson 6 Questions

  1. When you generate a scaffold, it creates:
  • A model file
  • A controller file with CRUD actions
  • Views for each CRUD action
  • A migration file
  1. 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.

  2. The rails db:migrate command runs the migration files, applying changes to the database schema, such as creating or altering tables.

  3. The current database schema can be seen in the db/schema.rb file.

  4. To show the routes, use the command rails routes.

  5. 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.

  6. 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.

  7. 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.

  8. 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.

  9. 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.

⚠️ **GitHub.com Fallback** ⚠️