如何在Cloud9上使用Rails Composer和PostgreSQL并在Heroku上部署 [英] How to use Rails Composer and postgreSQL on Cloud9 and deploy on Heroku

查看:143
本文介绍了如何在Cloud9上使用Rails Composer和PostgreSQL并在Heroku上部署的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 Rails Composer 创建一个使用postgreSQL的Rails应用程序的过程 Cloud9 并且在Heroku上部署充满了可能混淆新开发人员的警告和错误。是否有任何说明可以帮助我完成整个过程并部署我的入门应用程序? 我相信在rails作曲家中, cloud9和heroku以及后者推荐的postgreSQL数据库尽管一起使用它们有很多小细节,没有人能够澄清如何解决。花了很多时间来完成它之后,我创建了以下列出的指令,这些指令在发布日期和时间之后运行。


  • 创建cloud9上的新工作空间

  • 在您的工作空间中设置postgreSQL数据库:

    $ sudo service postgresql start p>

    $ sudo sudo -u postgres psql



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



    创建角色

    postgres =#\ q


>

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

  $ mkdir [您应用的目录/域名 ] 

$ cd [您应用程序的目录/域名]

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

创建一些局部变量并保存一些值。

$ echoexport USERNAME = username>> 〜/ .profile



$ echoexport PASSWORD = password>> 〜/ .profile



在cl上输入您的应用程序的域名作为本地环境变量:



$ echoexport DOMAIN_NAME = [domain name] .herokuapp.com>> 〜/ .profile



接下来,devise要求将超长密钥设置为环境变量。一旦创建了rails应用程序,rake就可以使用cl上的 $ rake secret 命令生成这样一个密钥。由于我们尚未使用composer来生成我们的rails应用程序,因此rake将不起作用。尽管如此,我们仍然会使用irb和securerandom,正如James Badger在他的博客文章中所描述的那样:为Rails应用程序生成一个新的秘密令牌

  $ irb 
2.2.3:001>要求'securerandom'
=>真
2.2.3:002> SecureRandom.hex(64)
=> 137d8b4bf436e670e2eea63372494b84aa25900edb1328eb5c1367f5100fe114fc95313f8772428dbda89ed84086e87a26428ef524951f94fd0375d4e399b613
2.2.3:003>退出

我们(你和我)应该在生产中使用不同的产品。

$ echoexport SECRET_KEY_BASE = 137d8b4bf436e670e2eea63372494b84aa25900edb1328eb5c1367f5100fe114fc95313f8772428dbda89ed84086e87a26428ef524951f94fd0375d4e399b613>> 〜/ .profile



RESTART TERMINAL ,以便设置ENV变量。在终端中右键单击并选择重启所有终端将在cloud9中执行。重新启动终端可能会使您返回到根目录。如果是这样的话,请使用以下命令切换到新的应用程序目录:

  $ cd [您的应用程序目录] 

现在您将全部加速 Rails Composer

  $ rails new。 -m https://raw.github.com/RailsApps/rails-composer/master/composer.rb 

接下来是我的轨道作曲家选择,虽然你可能会有所不同,但可能对你有用。

创建初学者应用程序?:3 - 选择创建自定义rails应用程序



开发服务器:4 - puma

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

开发数据库:2 - PostgresSQL



模板引擎:2 - Haml



测试环境:2 - rspec与水豚



连续测试:1 - 无

前端框架: - bootstrap 3.3



电子邮件支持:1 - 无

身份验证:2 - 设计



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



授权:1 - 无



表单构建器gem:2 - 简单表单



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


添加Bootstrap页面模板工商业污水附加费? [您可以在 startbootstrap.com 模板网页上查看选项。]



设置本地?:[返回/无英文版]



页面视图分析:2 - Google Analytics p>

在Google上生成了Google Analytics(分析)ID并将其输入。



准备部署:2 - heroku

禁用Rails Turbolinks?:n



禁用蜘蛛:n



创建github仓库:n - 我计划使用bitbucket



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



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

在开发过程中通过'better_errors'提高错误报告的质量? y



在开发和测试过程中使用'pry'作为控制台替换:y使用'rubocop'确保你的代码符合Ruby风格指南? y



创建项目特定的rvm gemset? y



添加'therubyracer'? n


$ b [创建应用程序!]



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



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



password#同样给出了我在上面输入的用于创建本地pg数据库的内容



好的,放弃所有名为[您的应用名称]的现有数据库? y



在git上提交最终更改
由于某种原因,Rails Composer似乎跳过了一个最终提交。以下内容:


  $ git add。 

$ git commit -amFinal rails_composer commit

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



遵循'开始使用rails'的说明包括以下内容:

  $ heroku登录

在提示处输入您的heroku凭证。

  $ heroku create [您的应用程序名称没有herokuapp.com作为本地变量存储在上面)] 

验证heroku remote是否添加了以下内容命令:

  $ git config --list | grep heroku 

$ git push heroku master

现在您已经创建在heroku上的应用程序,你也必须添加一些环境变量,使用下面的命令:

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

$ rake secret#使用rake生成一个新的密钥

5ed8c7d9a3bda9cec3887b61f22aa95bf430a3a550407642b96751c7ef0ce8946a161506d6739da0dcaaea8c8f4f8b3335b1fb549e3cc54f0a4cec554ede05f8



将该新密钥剪切并粘贴到下文命令设置的Heroku环境变量

  $ heroku上配置:设置SECRET_KEY_BASE = 5ed8c7d9a3bda9cec3887b61f22aa95bf430a3a550407642b96751c7ef0ce8946a161506d6739da0dcaaea8c8f4f8b3335b1fb549e3cc54f0a4cec554ede05f8 
$ rake secret
来产生一个不同于SECRET_KEY_BASE的SECRET_KEY_BASE 已经包含在上面的代码中。



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

  $ heroku run rake db:migrate 

创建一个Procfile

  $ touch Procfile 

[procfile]

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

$ touch config / puma.rb

[config / puma.rb]

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

preload_app!

rackup DefaultRackup
端口ENV ['PORT'] || 3000
环境ENV ['RACK_ENV'] || 'development'

on_worker_boot do
#Rails 4.1+的特定于工作人员的设置
#请参阅:https://devcenter.heroku.com/articles/deploying-rails-applications- with-the-puma-web-server#on-worker-boot
ActiveRecord :: Base.establish_connection
end

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

  $ heroku config:set MIN_THREADS = 1 MAX_THREADS = 1 

提交Procfile和美洲狮配置文件:

  $ git add。 

$ git commit -m添加Procfile和config / puma.rb

$ git push&& git push heroku

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


The process of using Rails Composer to create a rails app using postgreSQL on Cloud9 and deploying on Heroku is fraught with warnings and errors that can confound a new developer. Are there any instructions that can help me get through the process and deploy my starter app?

解决方案

I believe in the rails composer, cloud9 and heroku and the latter's recommended postgreSQL database though using them all together has a lot of little details that no one source clarifies how to get around. After spending many nights working it out, I've created the following list of instructions which works as of the publish date and time.

  • Create a new workspace on cloud9
  • Set up a postgreSQL database on your workspace:

    $ sudo service postgresql start

    $ sudo sudo -u postgres psql

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

    CREATE ROLE

    postgres=# \q

Choose a name of the app that is available on heroku since it is either one that you have or one still available on heroku. You can check availability by visiting [potential domain name].herokuapp.com

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

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

The current gems used by Rails Composer use ruby version 2.2.3 so that must be installed in rvm with the following code:

$ 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

Create some local variables and save some values to them.

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

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

Enter your app's domain name as a local environmental variable with the following code on the cl:

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

Next, devise requires that a super long secret key be set as an environmental variable. Once a rails app is created, rake can generate such a secret key using the $ rake secret command on the cl. Since we've not yet used composer to generate our rails app, rake won't work. We'll do the same thing, though, using irb and securerandom as described by James Badger in his blog post, Generate a New Secret Token for Rails Apps:

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

We (you and me) should use a different one in production.

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

RESTART TERMINAL so that ENV variables are set. Right-clicking in terminal and selecting ‘restart all terminals’ will do it in cloud9. Restarting your terminals may return you to the root directory. If that's the case, change to your new app directory at the cl using:

$ cd [your app directory]

Now you're all set to rev up Rails Composer

$ rails new . -m https://raw.github.com/RailsApps/rails-composer/master/composer.rb

What follows are my rails composer choices which may be useful to you though yours may vary.

build a starter app?: 3 -- elected to create custom rails app

development server: 4 - puma

production server: 1 - same as development

development database: 2 - PostgresSQL

template engine: 2 - Haml

test enviroment: 2 - rspec with capybara

continuous testing: 1 - none

frontend framework: 2 -- bootstrap 3.3

email support: 1 -- none

authentication: 2 -- devise

devise modules: 1 -- devise with default modules

authorization: 1 -- none

form builder gem: 2 -- simpleform

Add pages: 5 -- Home, About, and Users

Add Bootstrap page templates? [You can check out the options at the startbootstrap.com templates webpage.]

set a local?: [return/nothing for English]

page-view analytics: 2 -- Google Analytics

generated a google analytics ID on google and entered it in.

prepare for deployment: 2 -- heroku

Disable Rails Turbolinks?: n

ban spiders: n

create github repository: n -- I plan on using bitbucket

add gem and file for environmental variables?: 1

reduce assets logger noise during development: y

improve error reporting with ‘better_errors’ during development? y

use ‘pry’ as console replacement during development and test: y

use ‘rubocop’ to ensure that your code conforms to the Ruby Style guide? y

create a project-specific rvm gemset? y

Add ‘therubyracer’? n

[Creating app!]

Username for PostgreSQL (leave blank to use the app name) username # given what I entered above to create the local pg database

Host for PostgreSQL in database.yml? (leave blank to use default socket connection) left blank

password # also given what I entered above to create the local pg database

Okay to drop all existing databases named [your app name]? y

commit final changes on git There's one final commit that Rails Composer seems to skip for some reason... Do the following:

$ git add .

$ git commit -am "Final rails_composer commit"

follow directions on bitbucket to create new repository for existing project and push to it from command line

follow instructions for ‘getting started with rails’ on heroku which includes the following:

$ heroku login

Enter your heroku credentials at the prompts.

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

Verify heroku remote was added with the following command:

$ git config --list | grep heroku

$ git push heroku master

Now that you've created the app on heroku, you've got to add some environmental variables there, too, with the following commands:

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

$ rake secret    # uses rake to generate a new secret key

5ed8c7d9a3bda9cec3887b61f22aa95bf430a3a550407642b96751c7ef0ce8946a161506d6739da0dcaaea8c8f4f8b3335b1fb549e3cc54f0a4cec554ede05f8

Cut and paste that new secret key into the command below to set a Heroku environmental variable.

$ heroku config:set SECRET_KEY_BASE=5ed8c7d9a3bda9cec3887b61f22aa95bf430a3a550407642b96751c7ef0ce8946a161506d6739da0dcaaea8c8f4f8b3335b1fb549e3cc54f0a4cec554ede05f8

Note: You should use $ rake secret to generate a different SECRET_KEY_BASE than the one I've included in the code above.

Now you can migrate the heroku databases:

$ heroku run rake db:migrate

Create a Procfile

$ touch Procfile

[Procfile]

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

$ touch config/puma.rb

[config/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

Because I don't expect my app to get too much traffic and can't guarantee that it's thread-safe:

$ heroku config:set MIN_THREADS=1 MAX_THREADS=1

Commit that Procfile and puma configuration file with:

$ git add .

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

$ git push && git push heroku

I hope that these instructions are useful. Please feel free to add to them and improve them.

这篇关于如何在Cloud9上使用Rails Composer和PostgreSQL并在Heroku上部署的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆