引言
Ruby on Rails,简称Rails,是一个流行的Ruby语言Web应用框架,它遵循MVC(Model-View-Controller)设计模式。MVC模式将应用程序分为三个主要部分:模型(Model)、视图(View)和控制器(Controller)。这种模式有助于代码的组织和重用,使得Web开发更加高效。本文将带你从零开始学习Ruby MVC框架,并通过实战项目演示全攻略,让你快速掌握Rails开发。
第一部分:准备工作
1.1 安装Ruby
首先,你需要安装Ruby。你可以从官网(https://www.ruby-lang.org/zh_cn/downloads/)下载安装包,或者使用包管理器进行安装。
# 使用RVM安装Ruby
$ rvm install 2.7.2
$ rvm use 2.7.2 --default
1.2 安装Rails
安装Rails之前,确保你已经安装了Ruby。使用以下命令安装Rails:
$ gem install rails
1.3 创建新项目
使用以下命令创建一个新的Rails项目:
$ rails new myapp
进入项目目录:
$ cd myapp
第二部分:Rails基础
2.1 MVC模式
MVC模式将应用程序分为三个主要部分:
- 模型(Model):负责处理应用程序的数据逻辑,如数据库操作。
- 视图(View):负责显示数据,如HTML模板。
- 控制器(Controller):负责处理用户请求,并调用模型和视图。
2.2 Rails目录结构
Rails项目通常具有以下目录结构:
myapp/
|-- app/
| |-- controllers/
| |-- models/
| |-- views/
| |-- assets/
| |-- config/
| |-- db/
|-- Gemfile
|-- Gemfile.lock
|-- README.md
|-- Rakefile
|-- config.ru
|-- .gitignore
2.3 创建模型、视图和控制器
使用以下命令创建一个模型、视图和控制器:
$ rails generate model Article title:string content:text
$ rails generate controller Articles
第三部分:实战项目
3.1 项目需求
本项目将实现一个简单的博客系统,包括以下功能:
- 文章列表
- 文章详情
- 文章创建
- 文章编辑
- 文章删除
3.2 创建模型
首先,创建一个名为Article的模型,用于存储文章信息。
# app/models/article.rb
class Article < ApplicationRecord
validates :title, presence: true
validates :content, presence: true
end
3.3 创建控制器
接下来,创建一个名为articles的控制器,用于处理文章相关的请求。
# app/controllers/articles_controller.rb
class ArticlesController < ApplicationController
def index
@articles = Article.all
end
def show
@article = Article.find(params[:id])
end
def new
@article = Article.new
end
def create
@article = Article.new(article_params)
if @article.save
redirect_to @article
else
render :new
end
end
def edit
@article = Article.find(params[:id])
end
def update
@article = Article.find(params[:id])
if @article.update(article_params)
redirect_to @article
else
render :edit
end
end
def destroy
@article = Article.find(params[:id])
@article.destroy
redirect_to articles_path
end
private
def article_params
params.require(:article).permit(:title, :content)
end
end
3.4 创建视图
创建相应的视图文件,用于显示文章列表、文章详情、文章创建表单等。
<!-- app/views/articles/index.html.erb -->
<h1>文章列表</h1>
<ul>
<% @articles.each do |article| %>
<li><%= link_to article.title, article %></li>
<% end %>
</ul>
<%= link_to '新建文章', new_article_path %>
<!-- app/views/articles/show.html.erb -->
<h1><%= @article.title %></h1>
<p><%= @article.content %></p>
<%= link_to '编辑', edit_article_path(@article) %>
<%= link_to '删除', article_path(@article), method: :delete, data: { confirm: '确定要删除这篇文章吗?' } %>
<!-- app/views/articles/new.html.erb -->
<%= form_with(model: @article, local: true) do |form| %>
<div class="field">
<%= form.label :title %>
<%= form.text_field :title %>
</div>
<div class="field">
<%= form.label :content %>
<%= form.text_area :content %>
</div>
<div class="actions">
<%= form.submit %>
</div>
<% end %>
<!-- app/views/articles/edit.html.erb -->
<%= form_with(model: @article, local: true) do |form| %>
<div class="field">
<%= form.label :title %>
<%= form.text_field :title %>
</div>
<div class="field">
<%= form.label :content %>
<%= form.text_area :content %>
</div>
<div class="actions">
<%= form.submit %>
</div>
<% end %>
3.5 配置路由
在config/routes.rb文件中配置路由:
Rails.application.routes.draw do
resources :articles
end
3.6 运行项目
启动Rails服务器:
$ rails server
在浏览器中访问http://localhost:3000/articles,你将看到文章列表页面。
总结
通过本文的学习,你已掌握了Ruby MVC框架的基础知识,并通过实战项目演示了如何创建一个简单的博客系统。希望这篇文章能帮助你更好地理解Rails开发,祝你学习愉快!
