Ruby on Rails 开发笔记
安装 Ruby on Rails
- 安装 ruby
RubyInstall for Windows - 安装 sqlite
SQLite Download Page
下载 sqlite-tools-win32-x86-......zip - 安装 rails
gem install rails
创建应用
MacOS 平台
# 创建新的应用程序
$ rails new blog
$ cd blog
# 安装 yarn
$ brew install yarn
# 安装 webpacker
$ rails webpacker:install
# 启动服务器
$ rails server
# http://localhost:3000 可访问网页
Windows 平台
- 创建新的应用程序
- 安装 yarn
- 安装 node
- 安装 webpacker
官方示例
控制器生成器
使用 generate controller 命令(控制器生成器)来自动生成控制器(controller),视图(view) 以及动作(action)
# 生成 Welcome 控制器以及它的 index 动作
$ rails generate controller Welcome index
主要生成下面两个文件
- app/controllers/welcome_controller.rb
控制器文件 - app/views/welcome/index.html.erb
嵌入式Ruby文件 - http://localhost:3000/welcome/index
Rails 将把该请求映射为 Welcome 控制器的 index 动作
路由
通过修改 config/routes.rb 文件来设定路由
Rails.application.routes.draw do
get 'welcome/index'
resources :articles do
resources :comments
end
root 'welcome#index'
end
使用 routes 命令来确认路由
$ rails routes
Prefix Verb URI Pattern Controller#Action
welcome_index GET /welcome/index(.:format) welcome#index
article_comments GET /articles/:article_id/comments(.:format) comments#index
POST /articles/:article_id/comments(.:format) comments#create
new_article_comment GET /articles/:article_id/comments/new(.:format) comments#new
edit_article_comment GET /articles/:article_id/comments/:id/edit(.:format) comments#edit
article_comment GET /articles/:article_id/comments/:id(.:format) comments#show
PATCH /articles/:article_id/comments/:id(.:format) comments#update
PUT /articles/:article_id/comments/:id(.:format) comments#update
DELETE /articles/:article_id/comments/:id(.:format) comments#destroy
articles GET /articles(.:format) articles#index
POST /articles(.:format) articles#create
new_article GET /articles/new(.:format) articles#new
edit_article GET /articles/:id/edit(.:format) articles#edit
article GET /articles/:id(.:format) articles#show
PATCH /articles/:id(.:format) articles#update
PUT /articles/:id(.:format) articles#update
DELETE /articles/:id(.:format) articles#destroy
root GET / welcome#index
Articles Comments 控制器
# 生成 Articles 控制器
$ rails generate controller Articles
# 生成 Comments 控制器
$ rails generate controller Comments
Article Comment 模型
# 生成 Article 模型,然后创建 db
$ rails generate model Article title:string text:text
$ rails db:migrate
# 生成 Comment 模型,然后更新 db
$ rails generate model Comment commenter:string body:text article:references
$ rails db:migrate
模型生成器主要生成下面两种文件
- app/models/article.rb
app/models/comment.rb
模型文件 - db/migrate/20200504035451_create_articles.rb
db/migrate/20200504050736_create_comments.rb
数据迁移指令Ruby文件
模型类代码
app/models/article.rb 文件
class Article < ApplicationRecord
has_many :comments, dependent: :destroy
validates :title, presence: true,
length: { minimum: 5 }
end
app/models/comment.rb 文件
class Comment < ApplicationRecord
belongs_to :article
end
控制器类代码
app/controllers/articles_controller.rb 文件
class ArticlesController < ApplicationController
http_basic_authenticate_with name: "dhh", password: "secret", except: [:index, :show]
def index
@articles = Article.all
end
def show
@article = Article.find(params[:id])
end
def new
@article = Article.new
end
def edit
@article = Article.find(params[:id])
end
def create
@article = Article.new(article_params)
if @article.save
redirect_to @article
else
render 'new'
end
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, :text)
end
end
下面是该控制器类所包含的方法以及它们所对应的路由
- index(列表)GET /articles
- show(显示)GET /articles/:id
- new(新建)GET /articles/new
- edit(编辑)GET /articles/:id/edit
- create(创建)PATCH/POST /articles
- update(更新)PUT /articles/:id
- destroy(删除)DELETE /articles/:id
app/controllers/comments_controller.rb 文件
class CommentsController < ApplicationController
http_basic_authenticate_with name: "dhh", password: "secret", only: :destroy
def create
@article = Article.find(params[:article_id])
@comment = @article.comments.create(comment_params)
redirect_to article_path(@article)
end
def destroy
@article = Article.find(params[:article_id])
@comment = @article.comments.find(params[:id])
@comment.destroy
redirect_to article_path(@article)
end
private
def comment_params
params.require(:comment).permit(:commenter, :body)
end
end
下面是该控制器类所包含的方法以及它们所对应的路由
- create(创建)PATCH/POST /articles/:article_id/comments
- destroy(删除)DELETE /articles/:article_id/comments/:id
welcome 视图代码
app/views/welcome/index.html.erb 文件
<h1>Hello, Rails!</h1>
<%= link_to 'My Blog', controller: 'articles' %>
articles 视图代码
app/views/articles/_form.html.erb 文件
<%= form_with model: @article, local: true do |form| %>
<% if @article.errors.any? %>
<div id="error_explanation">
<h2>
<%= pluralize(@article.errors.count, "error") %> prohibited
this article from being saved:
</h2>
<ul>
<% @article.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>
<p>
<%= form.label :title %><br>
<%= form.text_field :title %>
</p>
<p>
<%= form.label :text %><br>
<%= form.text_area :text %>
</p>
<p>
<%= form.submit %>
</p>
<% end %>
app/views/articles/edit.html.erb 文件
<h1>Edit article</h1>
<%= render 'form' %>
<%= link_to 'Back', articles_path %>
app/views/articles/index.html.erb 文件
<h1>Listing articles</h1>
<%= link_to 'New article', new_article_path %>
<table>
<tr>
<th>Title</th>
<th>Text</th>
<th></th>
</tr>
<% @articles.each do |article| %>
<tr>
<td><%= article.title %></td>
<td><%= article.text %></td>
<td><%= link_to 'Show', article_path(article) %></td>
<td><%= link_to 'Edit', edit_article_path(article) %></td>
<td><%= link_to 'Destroy', article_path(article),
method: :delete,
data: { confirm: 'Are you sure?' } %></td>
</tr>
<% end %>
</table>
app/views/articles/new.html.erb 文件
<h1>New article</h1>
<%= render 'form' %>
<%= link_to 'Back', articles_path %>
app/views/articles/show.html.erb 文件
<p>
<strong>Title:</strong>
<%= @article.title %>
</p>
<p>
<strong>Text:</strong>
<%= @article.text %>
</p>
<h2>Comments</h2>
<%= render @article.comments %>
<h2>Add a comment:</h2>
<%= render 'comments/form' %>
<%= link_to 'Edit', edit_article_path(@article) %> |
<%= link_to 'Back', articles_path %>
comments 视图代码
app/views/articles/_comment.html.erb 文件
<p>
<strong>Commenter:</strong>
<%= comment.commenter %>
</p>
<p>
<strong>Comment:</strong>
<%= comment.body %>
</p>
<p>
<%= link_to 'Destroy Comment', [comment.article, comment],
method: :delete,
data: { confirm: 'Are you sure?' } %>
</p>
app/views/articles/_form.html.erb 文件
<%= form_with(model: [ @article, @article.comments.build ], local: true) do |form| %>
<p>
<%= form.label :commenter %><br>
<%= form.text_field :commenter %>
</p>
<p>
<%= form.label :body %><br>
<%= form.text_area :body %>
</p>
<p>
<%= form.submit %>
</p>
<% end %>
参考链接
Ruby on Rails 开发笔记的更多相关文章
- [Ruby on Rails系列]2、开发环境准备:Ruby on Rails开发环境配置
前情回顾 上次讲到Vmware虚拟机的安装配置以及Scientific Linux 6.X系统的安装.这回我们的主要任务是在Linux操作系统上完成Ruby on Rails开发环境的配置. 在配置环 ...
- 开发环境准备:Ruby on Rails开发环境配置
开发环境准备:Ruby on Rails开发环境配置 前情回顾 上次讲到Vmware虚拟机的安装配置以及Scientific Linux 6.X系统的安装.这回我们的主要任务是在Linux操作系统上完 ...
- 在linux,windows上安装ruby on rails开发环境
ruby是一个非常优秀的语言,ruby的精髓rails可以让web开发的效率成倍的提高,下面就介绍一下我搭建rails环境的过程.windows下搭建ruby rails web开发环境本篇文章主要是 ...
- 各种环境下搭建ruby on rails开发环境
win10上搭建raby on rails环境: 步骤如下 1.安装ruby (我选择的版本是ruby 2.2.3p173) 2.安装rails gem 在这之前建议先把gem的源换成淘宝的源,速度快 ...
- Ruby on Rails开发Web应用的基本概念
Web应用架构 C/S架构 Web应用从最初就採用C/S架构.Server负责监听client请求,提供资源,Client向server发起请求并渲染页面.两者通过TCP/IP协议栈之上的HTTP协议 ...
- 用VirtualBox和vagrant在win7×64上搭建ruby on rails 开发环境
下载准备 1.vagrant 官方 WINDOWS Universal (32 and 64-bit) http://www.vagrantup.com/downloads.html 2.Virtu ...
- [QuickRoR]Ruby on Rails开发环境安装
1.Setup Ruby on Rails2.Test Web App3.Create the First Web App 1.Setup Ruby on Rails1) Download rubyi ...
- win7安装ruby on rails开发环境
前言 我们看到很多文章说ruby环境windows它是非常困难的基础上,这将是各种稀奇古怪的问题,因此,建议linux和mac发. 可是我依照教程搭了下,问题也不算太多.总过大概花费了2个半小时左右就 ...
- Ruby on rails学习笔记——安装环境
出现问题: C:\Users\len>gem install rails ERROR: While executing gem ... (Gem::RemoteFetcher::FetchErr ...
随机推荐
- gunicorn启动flask项目的坑
问题描述:项目用的是flask框架,在项目上线的时候,服务器上是使用gunicorn来启动项目的.但是上线之后,发现服务成功启动了,也有正确的返回值,但是没有生成日志,而用python来启动服务的时候 ...
- 用户id,组id和文件访问权限
实际用户ID和实际组ID:标示了我们究竟是谁,这两个字段在登录时取自口令文件中的登录项 有效用户ID和有效组ID以及附属组ID:决定了我们的文件的访问权限(通常有效用户ID等于实际用户ID,有效组ID ...
- Linux colrm命令详解
Linux colrm命令 colrm用于从文件或标准输入中过滤掉指定的列.从标准输入设备读取书记,转而输出到标准输出设备.如果不加任何参数,则该指令不会过滤任何一行. 语法: colrm 参数 参数 ...
- VS2015 使用GIT同步到github
https://www.cnblogs.com/newP/p/5732431.html(参考) 拉取(Pull):将远程版本库合并到本地版本库,相当于(Fetch+Meger) 获取(Fetch):从 ...
- eclipse项目上传服务器注意事项
1.先准备服务器环境 2 数据库导入,tomcat安装 3 开放服务器端口,配置网络 4.修改本地代码修改为发布版本,即ip,账号密码数据库等 5 变动的信息应该写在配置文件或者一个全局产量中,这样才 ...
- CentOS 与Ubuntu 下配置IP地址
1.CentOS配置方法如下: 用vi打开配置文件 [root@haha3 ~]# vi /etc/sysconfig/network-scripts/ifcfg-eth0 写入以下配置 DEVIC ...
- dis集群研究和实践(基于redis 3.0.5) 《转载》
https://www.cnblogs.com/wxd0108/p/5798498.html 前言 redis 是我们目前大规模使用的缓存中间件,由于它强大高效而又便捷的功能,得到了广泛的使用.现在的 ...
- day2.jmeter简单压测,下载文件,Charles手机抓包准备
一.压测 压测衡量一个系统的好坏:1.tps每秒钟处理的事物数,2.qps响应时间 添加聚合报告,更改线程组,运行接口请求 **添加压力机 1.首先确保都在同一网段 2.其他电脑要先启动jmeter- ...
- linux 终端 pac ssh登录工具使用教程
1.下载: 首先下载deb文件:https://sourceforge.net/projects/pacmanager/ 2.安装: dpkg -i pac-4.5.5.7-all.deb 3.安装依 ...
- Nginx——基本操作
1.获得root用户权限 如果鉴定失败多试几次 一. gcc 安装安装 nginx 需要先将官网下载的源码进行编译,编译依赖 gcc 环境,如果没有 gcc 环境,则需要安装: yum install ...