In this section, we will walk through the process of creating a simple Ruby on Rails application. By the end of this lesson, you will have a basic understanding of how to set up a Rails project, generate a scaffold, and run your application.

Prerequisites

Before we start, ensure you have:

  • Ruby installed on your system.
  • Rails gem installed (gem install rails).
  • A basic understanding of Ruby programming.

Steps to Create a Simple Rails Application

  1. Setting Up a New Rails Project

To create a new Rails application, open your terminal and run the following command:

rails new my_first_app

This command will generate a new Rails project named my_first_app with a standard directory structure and necessary files.

  1. Navigating to Your Project Directory

Navigate to your newly created project directory:

cd my_first_app

  1. Running the Rails Server

To see your application in action, start the Rails server by running:

rails server

By default, the server will run on http://localhost:3000. Open your web browser and navigate to this URL. You should see the Rails welcome page.

  1. Generating a Scaffold

Rails scaffolding is a quick way to generate the basic CRUD (Create, Read, Update, Delete) operations for a resource. Let's create a scaffold for a simple Post resource with a title and body:

rails generate scaffold Post title:string body:text

This command will generate the following:

  • A model file (app/models/post.rb)
  • A migration file to create the posts table in the database
  • Controller files (app/controllers/posts_controller.rb)
  • Views for the CRUD operations (app/views/posts/)
  • Routes for the Post resource

  1. Running Migrations

To apply the generated migration and create the posts table in your database, run:

rails db:migrate

  1. Exploring the Generated Code

Let's take a look at the generated files:

  • Model (app/models/post.rb): Defines the Post model.
  • Controller (app/controllers/posts_controller.rb): Contains actions for handling HTTP requests.
  • Views (app/views/posts/): Contains HTML templates for displaying posts.
  • Routes (config/routes.rb): Defines the routes for the Post resource.

  1. Accessing the Scaffolded Resource

With the server still running, navigate to http://localhost:3000/posts in your web browser. You should see a list of posts (which will be empty initially). You can create, view, edit, and delete posts using the generated interface.

  1. Customizing the Scaffold

You can customize the scaffolded views and controller actions to fit your application's needs. For example, you can modify the form in app/views/posts/_form.html.erb to include additional fields or change the layout.

Practical Exercise

Task: Create a new Rails application and generate a scaffold for a Book resource with the following attributes:

  • title (string)
  • author (string)
  • description (text)

Steps:

  1. Create a new Rails project named library.
  2. Generate a scaffold for the Book resource.
  3. Run the migrations.
  4. Start the Rails server and navigate to http://localhost:3000/books to interact with the scaffolded resource.

Solution:

  1. Create a new Rails project:

    rails new library
    cd library
    
  2. Generate the scaffold:

    rails generate scaffold Book title:string author:string description:text
    
  3. Run the migrations:

    rails db:migrate
    
  4. Start the Rails server:

    rails server
    
  5. Navigate to http://localhost:3000/books in your web browser.

Common Mistakes and Tips

  • Missing Migrations: Ensure you run rails db:migrate after generating a scaffold to apply the changes to your database.
  • Server Not Running: If you can't access http://localhost:3000, make sure the Rails server is running.
  • Syntax Errors: Double-check your code for typos or syntax errors, especially when customizing the scaffold.

Conclusion

In this lesson, you learned how to create a simple Rails application, generate a scaffold, and run your application. You now have a basic understanding of how Rails handles CRUD operations and how to customize the generated code. In the next lesson, we will dive deeper into the MVC architecture of Rails and explore how to build more complex applications.

© Copyright 2024. All rights reserved