如何在Cloud9上使用Rails Composer和postgreSQL并在Heroku上部署

Ben*_*enU 4 postgresql heroku cloud9-ide ruby-on-rails-4 rails-composer

使用Rails ComposerCloud9上使用postgreSQL创建rails应用程序并在Heroku 上部署的过程充满了警告和错误,这可能会让新开发人员感到困惑.是否有任何说明可以帮助我完成整个过程并部署我的入门应用程序?

Ben*_*enU 8

我相信rails composer,cloud9和heroku以及后者推荐的postgreSQL数据库,尽管它们一起使用它们有很多细节,没有任何一个来源澄清如何绕过.在花了很多夜的时间之后,我创建了以下列出的说明,这些说明在发布日期和时间之后有效.

  • 在cloud9上创建一个新工作区
  • 在工作区上设置postgreSQL数据库:

    $ sudo service postgresql start

    $ sudo sudo -u postgres psql

    postgres = #CREATE USER username SUPERUSER PASSWORD'password';

    创造角色

    postgres =#\ q

选择heroku上可用的应用程序的名称,因为它是您拥有的应用程序或者仍然可以在heroku上使用的应用程序.您可以访问[潜在域名] .herokuapp.com来查看可用性

$ mkdir ["your app's directory/domain name"]

$ cd ["your app's directory/domain name"]
Run Code Online (Sandbox Code Playgroud)

Rails Composer使用的当前gem使用ruby 2.2.3版,因此必须使用以下代码安装在rvm中:

$ rvm install 2.2.3

$ ruby -v

ruby 2.2.3p173 (2015-08-18 revision 51636) [x86_64-linux]

$ rvm use ruby-2.2.3@[your app name] --ruby-version --create

$ gem install rails --no-ri --no-rdoc
Run Code Online (Sandbox Code Playgroud)

创建一些局部变量并为它们保存一些值.

$ echo "export USERNAME=username" >> ~/.profile

$ echo "export PASSWORD=password" >> ~/.profile

输入您的应用程序的域名作为本地环境变量,并在cl上使用以下代码:

$ echo "export DOMAIN_NAME=[domain name].herokuapp.com" >> ~/.profile

接下来,设计要求将超长密钥设置为环境变量.一旦创建了rails应用程序,rake就可以使用$ rake secretcl上的命令生成这样的密钥.由于我们还没有使用composer来生成我们的rails应用程序,因此rake不起作用.不过,我们将使用irb和securerandom做同样的事情,正如James Badger在他的博客文章" 为Rails应用程序生成一个新的秘密令牌:

$ irb
2.2.3 :001 > require 'securerandom'
=> true 
2.2.3 :002 > SecureRandom.hex(64)
=> "137d8b4bf436e670e2eea63372494b84aa25900edb1328eb5c1367f5100fe114fc95313f8772428dbda89ed84086e87a26428ef524951f94fd0375d4e399b613"
2.2.3 :003 > exit
Run Code Online (Sandbox Code Playgroud)

我们(你和我)应该在生产中使用不同的一个.

$ echo "export SECRET_KEY_BASE=137d8b4bf436e670e2eea63372494b84aa25900edb1328eb5c1367f5100fe114fc95313f8772428dbda89ed84086e87a26428ef524951f94fd0375d4e399b613" >> ~/.profile

RESTART TERMINAL以便设置ENV变量.右键单击终端并选择"重新启动所有终端"将在cloud9中执行.重新启动终端可能会返回到根目录.如果是这种情况,请使用以下命令更改为cl处的新app目录:

$ cd [your app directory]
Run Code Online (Sandbox Code Playgroud)

现在你已经准备好加速Rails Composer了

$ rails new . -m https://raw.github.com/RailsApps/rails-composer/master/composer.rb
Run Code Online (Sandbox Code Playgroud)

以下是我的rails作曲家选择,虽然你的可能会有所不同,但对你有用.

构建一个入门应用程序?:3 - 选择创建自定义rails应用程序

开发服务器:4 - puma

生产服务器:1 - 与开发相同

开发数据库:2 - PostgresSQL

模板引擎:2 - Haml

测试环境:2 - 与水豚的rspec

连续测试:1 - 无

前端框架:2 - bootstrap 3.3

电子邮件支持:1 - 无

身份验证:2 - 设计

设计模块:1 - 设计默认模块

授权:1 - 无

表单生成器gem:2 - simpleform

添加页面:5 - 主页,关于和用户

添加Bootstrap页面模板?[您可以在startbootstrap.com模板网页上查看选项.]

设置本地?:[返回/没有英语]

网页浏览分析:2 - Google Analytics

在谷歌上生成谷歌分析ID并输入.

准备部署:2 - heroku

禁用Rails Turbolinks?:n

禁蜘蛛

创建github存储库:n - 我打算使用bitbucket

为环境变量添加gem和文件?:1

在开发过程中减少资产记录器噪音:y

在开发过程中使用'better_errors'改进错误报告?ÿ

在开发和测试期间使用'pry'作为控制台替换:y

使用'rubocop'来确保您的代码符合Ruby Style指南?ÿ

创建一个特定于项目的rvm gemset?ÿ

添加'therubyracer'?ñ

[创建应用!]

PostgreSQL的用户名(留空以使用应用程序名称)用户名#给定我在上面输入的内容以创建本地pg数据库

在database.yml中托管PostgreSQL?(留空以使用默认套接字连接)留空

密码#也给出了我上面输入的内容来创建本地pg数据库

可以删除所有名为[您的应用名称]的现有数据库吗?ÿ

在git上提交最终更改 由于某种原因,Rails Composer似乎跳过了一个最终提交...执行以下操作:

$ git add .

$ git commit -am “Final rails_composer commit”
Run Code Online (Sandbox Code Playgroud)

按照bitbucket上的指示为现有项目创建新的存储库,并从命令行推送到它

按照heroku上的"入门指南"说明,其中包括以下内容:

$ heroku login
Run Code Online (Sandbox Code Playgroud)

根据提示输入您的heroku凭据.

$ heroku create [your app name (without the herokuapp.com stored as a local variable above)]
Run Code Online (Sandbox Code Playgroud)

使用以下命令验证heroku远程已添加:

$ git config --list | grep heroku

$ git push heroku master
Run Code Online (Sandbox Code Playgroud)

现在您已经在heroku上创建了应用程序,您还必须使用以下命令在那里添加一些环境变量:

$ heroku config:set DOMAIN_NAME=[your app name].herokuapp.com

$ rake secret    # uses rake to generate a new secret key
Run Code Online (Sandbox Code Playgroud)

5ed8c7d9a3bda9cec3887b61f22aa95bf430a3a550407642b96751c7ef0ce8946a161506d6739da0dcaaea8c8f4f8b3335b1fb549e3cc54f0a4cec554ede05f8

将新密钥剪切并粘贴到下面的命令中以设置Heroku环境变量.

$ heroku config:set SECRET_KEY_BASE=5ed8c7d9a3bda9cec3887b61f22aa95bf430a3a550407642b96751c7ef0ce8946a161506d6739da0dcaaea8c8f4f8b3335b1fb549e3cc54f0a4cec554ede05f8
Run Code Online (Sandbox Code Playgroud)

注意:您应该使用$ rake secret生成与上面代码中包含的SECRET_KEY_BASE不同的SECRET_KEY_BASE.

现在您可以迁移heroku数据库:

$ heroku run rake db:migrate
Run Code Online (Sandbox Code Playgroud)

创建一个Procfile

$ touch Procfile
Run Code Online (Sandbox Code Playgroud)

[Procfile]

web: bundle exec puma -C config/puma.rb

$ touch config/puma.rb
Run Code Online (Sandbox Code Playgroud)

[配置/ puma.rb]

workers Integer(ENV['WEB_CONCURRENCY'] || 2)
threads_count = Integer(ENV['MAX_THREADS'] || 5)
threads threads_count, threads_count

preload_app!

rackup      DefaultRackup
port        ENV['PORT']     || 3000
environment ENV['RACK_ENV'] || 'development'

on_worker_boot do
  # Worker specific setup for Rails 4.1+
  # See: https://devcenter.heroku.com/articles/deploying-rails-applications-with-the-puma-web-server#on-worker-boot
  ActiveRecord::Base.establish_connection
end
Run Code Online (Sandbox Code Playgroud)

因为我不希望我的应用程序获得太多流量,并且不能保证它的线程安全:

$ heroku config:set MIN_THREADS=1 MAX_THREADS=1
Run Code Online (Sandbox Code Playgroud)

提交Procfile和puma配置文件:

$ git add .

$ git commit -m "Add Procfile and config/puma.rb"

$ git push && git push heroku
Run Code Online (Sandbox Code Playgroud)

我希望这些说明很有用.请随意添加并改进它们.