Cài Đặt Môi Trường Phát Triển
Sử dụng trình quản lý phiên bản Ruby (rbenv hoặc RVM) để cài đặt Ruby. Với hệ điều hành macOS/Linux:
# Cài đặt Ruby qua rbenv
rbenv install 3.2.2
rbenv global 3.2.2
# Cài đặt Rails và SQLite
gem install rails sqlite3
Khởi Tạo Dự Án Mẫu
Tạo ứng dụng blog với cấu trúc tiêu chuẩn:
rails new blog_app --database=sqlite3
cd blog_app
bin/rails db:create
bin/rails server
Truy cập http://localhost:3000 sau khi máy chủ khởi động thành công.
Thiết Lập Bộ Điều Khiển Chính
Tạo bộ điều khiển chào mừng với hành động khởi tạo:
bin/rails generate controller Main welcome
Hệ thống sẽ sinh ra:
app/controllers/main_controller.rbapp/views/main/welcome.html.erb
Cấu Hình Định Tuyến
Chỉnh sửa config/routes.rb để thiết lập luồng ứng dụng:
Rails.application.routes.draw do
get 'trang_chu', to: 'main#welcome'
resources :bai_viets do
resources :binh_luan, only: [:create, :destroy]
end
root 'main#welcome'
end
Kiểm tra định tuyến bằng lệnh:
bin/rails routes | grep bai_viets
Thiết Kế Mô Hình Dữ Liệu
Tạo các mô hình bài viết và bình luận:
bin/rails generate model BaiViet tieu_de:string noi_dung:text
bin/rails generate model BinhLuan tac_gia:string noi_dung:text bai_viet:references
bin/rails db:migrate
Cài Đặt Quan Hệ Mô Hình
Trong app/models/bai_viet.rb:
class BaiViet < ApplicationRecord
has_many :binh_luan, dependent: :destroy
validates :tieu_de, presence: true, length: { minimum: 5 }
end
Trong app/models/binh_luan.rb:
class BinhLuan < ApplicationRecord
belongs_to :bai_viet
end
Xây Dựng Bộ Điều Khiển Bài Viết
Thực thi lệnh sinh bộ điều khiển:
bin/rails generate controller BaiViet
Triển khai phương thức trong app/controllers/bai_viet_controller.rb:
class BaiVietController < ApplicationController
http_basic_authenticate_with name: "admin", password: "12345", except: [:index, :show]
def index
@danh_sach = BaiViet.all
end
def show
@bai_viet = BaiViet.find(params[:id])
end
def create
@bai_viet = BaiViet.new(bai_viet_params)
@bai_viet.save ? redirect_to(@bai_viet) : render(:new)
end
# Các phương thức khác (new, edit, update, destroy) được triển khai tương tự
private
def bai_viet_params
params.require(:bai_viet).permit(:tieu_de, :noi_dung)
end
end
Triển Khai Giao Diện
Tập tin mẫu app/views/bai_viet/_form.html.erb:
<%= form_with model: @bai_viet do |f| %>
<% if @bai_viet.errors.any? %>
<div class="error-messages">
<h3><%= @bai_viet.errors.count %> lỗi xảy ra:</h3>
<ul>
<% @bai_viet.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>
<div class="field">
<%= f.label :tieu_de %>
<%= f.text_field :tieu_de %>
</div>
<div class="field">
<%= f.label :noi_dung %>
<%= f.text_area :noi_dung %>
</div>
<div class="actions">
<%= f.submit "Lưu bài viết" %>
</div>
<% end %>
Trang danh sách app/views/bai_viet/index.html.erb:
<h1>Danh sách bài viết</h1>
<%= link_to "Bài viết mới", new_bai_viet_path %>
<table>
<thead>
<tr>
<th>Tiêu đề</th>
<th>Nội dung</th>
<th colspan="3"></th>
</tr>
</thead>
<tbody>
<% @danh_sach.each do |bai| %>
<tr>
<td><%= bai.tieu_de %></td>
<td><%= truncate(bai.noi_dung, length: 50) %></td>
<td><%= link_to 'Xem', bai %></td>
<td><%= link_to 'Sửa', edit_bai_viet_path(bai) %></td>
<td><%= link_to 'Xóa', bai, method: :delete, data: { confirm: 'Xác nhận xóa?' } %></td>
</tr>
<% end %>
</tbody>
</table>