本地新建项目
cd # 进入家目录
mkdir workspace # 新建 workspace 目录
cd workspace/ # 进入 workspace 目录
rails _5.0.1_ new my_app
生成文件之后,在 Gemfile 文件中为所有 gem 指定精确的版本号
# 此处后续,要 install 可先翻墙,或者 更换资源,速度快一些
source 'https://rubygems.org'
gem 'rails', '5.0.1'
gem 'puma', '3.4.0'
gem 'sass-rails', '5.0.6'
gem 'uglifier', '3.0.0'
gem 'coffee-rails', '4.2.1'
gem 'jquery-rails', '4.1.1'
gem 'turbolinks', '5.0.1'
gem 'jbuilder', '2.4.1'
group :development, :test do
gem 'sqlite3', '1.3.12'
gem 'byebug', '9.0.0', platform: :mri
end
group :development do
gem 'web-console', '3.1.1'
gem 'listen', '3.0.8'
gem 'spring', '1.7.2'
gem 'spring-watcher-listen', '2.0.0'
end
group :production do
gem 'pg', '0.18.4'
end
gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby]
接着, bundle install
# 在my_app文件中
bundle install
# 接着 rails server
可正常运行之后,搭建 Heroku 部署环境
# Heroku 使用 PostgreSQL数据库,因此我们要在生产环境安装 pg gem,这样 Rails 才能与 PostgreSQL 通信
# 所以要加入pg gem,还要把sqlite3 gem处理一下,以免产生冲突
# 这里的操作在 Gemfile 进行
vim Gemfile
# 新增pg gem 至生产环境
group :production do
gem 'pg', '0.18.4'
end
# 将 sqlite3 移入开发环境[原有代码,修改]
group :development, :test do
gem 'sqlite3', '1.3.12'
gem 'byebug', '9.0.0', platform: :mri
end
执行 bundle install 命令,并且指定一个特殊的选项,禁止在本地安装生产环境使用的 gem(即 pg)
bundle install --without production
开始安装Heroku 【注册 Heroku 账户】
# 先看是否存在
heroku version
# 没有就安装
add-apt-repository "deb https://cli-assets.heroku.com/branches/stable/apt ./"
curl -L https://cli-assets.heroku.com/apt/release.key | sudo apt-key add -
apt-get update
apt-get install heroku
# 确认 Heroku 命令行工具已经安装之后,使用 heroku 命令登录,然后添加 SSH 密钥
heroku login
heroku keys:add
# 在 Heroku 的服务器中创建一个位置
heroku create
之后,会生成下面所示的代码。
你的二级域名,存放项目的git仓库。
Creating app... done, fathomless-beyond-39164
https://damp-fortress-5769.herokuapp.com/ |
https://git.heroku.com/damp-fortress-5769.git
然后利用git推送代码上这个git仓库。
git push heroku master
然后,就可以访问那个生成给你的二级域名,显示你的项目了。
附:
heroku 重命名你的应用【项目】
heroku rename rails-tutorial-hello
heroku 这个命令在你的项目访问出错的时候,可以帮助你找原因。前提是你的项目部署成功了。
heroku run rails console
参考:
https://railstutorial-china.org/book/chapter1.html#the-hello-application
https://devcenter.heroku.com/articles/heroku-cli
http://stackoverflow.com/questions/13496827/heroku-deployment-error-h10-app-crashed?answertab=votes#tab-top