Ruby on Rails Tutorial 第一章笔记 搭建开发环境 安装 Rails MVC架构模式 ( Rails 的工作方式) 添加 action 与 route 使用 GIt 使用 Heroku 部署应用

作者介绍了 Cloud9 Coding.net 这样的云端开发环境

安装 Rails

1. 新建 rails 应用

首先,调用 rails new 命令创建一个新的 Rails 应用, 格式如下:
rails new hello_app

2. 创建本地服务器

使用 rails server 命令创建一个服务器,

MVC架构模式 ( Rails 的工作方式)

MVC 是 model-view-controller 的缩写,
具体的流程如下:

  1. 客户端访问网站,向服务器发送请求
  2. 服务器将其转给 Rails 应用的 controller
  3. controller 与 model 交互( model 是一个 Ruby 对象,其与数据库进行通信)
  4. 之后, controller 渲染出 view, 生成 HTML 文件
  5. controller 将 HTML 发送给客户端

    Ruby on Rails Tutorial 第一章笔记
搭建开发环境
安装 Rails
MVC架构模式 ( Rails 的工作方式)
添加 action 与 route
使用 GIt
使用 Heroku 部署应用

添加 action 与 route

在 app/controllers/application_controller.rb 中添加动作
定义响应的函数,比如:

router 定义:
router (路由器) 在 controller 之前,用于决定客户端发送的请求由哪个动作来处理.
然后在 config/routes.rb 定义路由,代码如下:

使用 GIt

1. 新建仓库

格式:
git init

2. 跟踪所有文件

格式:
git add -A

3. 保存改动

格式:
git commit -m "Initialize repository"

使用 Bitbucket

1. 新建 SSH 公匙

命令:
cat ~/.ssh/id_rsa.pub

2. 从命令行添加项目

命令:
git remote add origin git@bitbucket.org:/hello_app.git
推送命令:
git push -u origin --all
(--all 写为 master 也可)

使用 Heroku 部署应用

1. 登陆 heroku

  1. 注册 Heroku 账号
  2. 在命令行中登陆 Heroku, 并添加密码: heroku login /heroku keys:add

2. 创建 heroku 应用

命令: heroku create

3. 将 GIt 主分支推送到 heroku 当中

命令: git push heroku master