学习 Ruby on Rails:快速入门与开发教程 – wiki基地

I am unable to write the tutorial content to a file due to an issue with the write_file tool. However, I can provide the content directly here:

Ruby on Rails Quick Start Development Tutorial

Introduction

Ruby on Rails, often simply called Rails, is a server-side web application framework written in Ruby. It provides a default structure for a database, a web service, and web pages. It encourages and facilitates the use of web standards such as JSON or XML for data transfer, and HTML, CSS and JavaScript for display and user interfacing. Rails emphasizes convention over configuration (CoC) and don’t repeat yourself (DRY), aiming to boost developer productivity.

This tutorial will guide you through setting up a basic Rails application, demonstrating the core concepts to get you started with web development using Rails.

Prerequisites

Before you begin, ensure you have the following installed on your system:

  • Ruby: Rails is built on Ruby, so you’ll need a recent version (e.g., 3.0 or newer). We recommend using a Ruby version manager like rbenv or RVM.
  • SQLite3: This is the default database for new Rails projects and is usually pre-installed or easily installable.
  • Node.js and Yarn: Rails 7 uses Node.js and Yarn for managing JavaScript dependencies.

1. Installation

Install Ruby (if not already installed)

Using rbenv (recommended):

“`bash
git clone https://github.com/rbenv/rbenv.git ~/.rbenv
echo ‘export PATH=”$HOME/.rbenv/bin:$PATH”‘ >> ~/.bashrc
echo ‘eval “$(rbenv init -)”‘ >> ~/.bashrc
source ~/.bashrc

git clone https://github.com/rbenv/ruby-build.git ~/.rbenv/plugins/ruby-build

rbenv install 3.2.2 # Or your preferred stable version
rbenv global 3.2.2
ruby -v
“`

Install Rails

Once Ruby is installed, you can install Rails using the gem command:

bash
gem install rails
rails -v

Install Node.js and Yarn

Follow the official installation guides for Node.js and Yarn for your operating system.

2. Creating a New Project

Navigate to the directory where you want to create your project and run the rails new command. This will create a new Rails application with a default directory structure.

bash
rails new my_first_app
cd my_first_app

This command creates a new directory my_first_app with all the necessary files and folders for a Rails application.

3. Database Setup

Rails uses Active Record (an ORM) to interact with databases. By default, new Rails applications use SQLite3. You don’t usually need to do much initial setup for SQLite3 beyond ensuring it’s installed.

To create the database for your application, run:

bash
rails db:create

4. Generating a Scaffold

A scaffold in Rails is a quick way to generate a full set of model, view, controller, and helper files for a new resource. It’s great for quickly getting a CRUD (Create, Read, Update, Delete) interface up and running.

Let’s create a simple Post resource with a title (string) and content (text).

bash
rails generate scaffold Post title:string content:text

After generating the scaffold, you need to run database migrations to create the posts table in your database:

bash
rails db:migrate

5. Running the Application

Now that you have a basic application and a scaffolded resource, you can start the Rails development server.

bash
rails server

Open your web browser and navigate to http://localhost:3000. You should see the default Rails welcome page.

To see your Post resource, go to http://localhost:3000/posts. You can now create, view, edit, and delete posts through this interface.

6. Basic Concepts (Brief Overview)

Rails follows the Model-View-Controller (MVC) architectural pattern:

  • Model: Handles the business logic and interacts with the database. In our example, app/models/post.rb defines the Post model.
  • View: Responsible for presenting data to the user. app/views/posts/ contains the HTML templates for displaying posts.
  • Controller: Acts as an intermediary between the Model and View, handling user input and updating the Model or View accordingly. app/controllers/posts_controller.rb handles requests related to posts.

Other key components:

  • Routes: Defined in config/routes.rb, they map incoming URL requests to controller actions.
  • Migrations: Files in db/migrate/ that define changes to your database schema.

7. Next Steps

This tutorial only scratches the surface of what Rails can do. To continue your learning, explore these topics:

  • Understanding MVC in depth: Dive deeper into how models, views, and controllers interact.
  • Routing: Learn how to define custom routes.
  • Forms: Customize forms for creating and editing resources.
  • Validations: Add rules to your models to ensure data integrity.
  • Associations: Define relationships between different models (e.g., a User has many Posts).
  • Authentication and Authorization: Secure your application.
  • Testing: Rails has built-in testing frameworks.
  • Front-end with JavaScript: Integrate more complex front-end logic.

Happy coding with Ruby on Rails!

滚动至顶部