ruby-on-rails - 使用 mongolab 的 Heroku 的 mongodb.yml

标签 ruby-on-rails heroku mongoid mlab

我尝试使用 mongolab (仅限 Rails 框架)将新的 Rails 应用程序部署到 Heroku

$ rails new myapp --skip-active-record
$ cd myapp

b.将 gem 添加到 Gemfile

ruby '2.2.0'
gem 'mongoid', '~> 4', github: 'mongoid/mongoid'
gem 'bson_ext'
group :production do
  gem 'rails_12factor'
end

c.捆绑这些 gem 并生成 mongoid.yml

$ bundle install
$ rails g mongoid:config

d.配置 mongoid.yml 添加/修改生产以使用 mongolab

mongoid.yml

production:
  sessions:
    default:
      uri: <%= ENV['MONGOLAB_URI'] %>

e.创建 git 和 heroku 应用程序

$ git init
$ git add .
$ git commit -am 'Initial Commit'  

$ heroku login
$ heroku apps:create myapp

f.蒙古实验室插件

$ heroku addons:add mongolab

以及mongolab沙箱显示:

mongolab 沙箱:

To connect using the shell:
mongo ds051831.mongolab.com:51831/heroku_app34606139 -u <dbuser> -p <dbpassword>
To connect using a driver via the standard URI (what's this?):
mongodb://<dbuser>:<dbpassword>@ds051831.mongolab.com:51831/heroku_app34606139

g。配置我的插件凭据

$ heroku config | grep MONGOLAB_URI

MONGOLAB_URI: mongodb://heroku_app34655737:nm2qauig0siu380pjntm0ov64q@ds051831.mongolab.com:51831/heroku_app34655737

然后从mongodb://复制......

$ heroku config:add MONGOLAB_URI=mongodb://my_mongolab_user:my_mongolab_password@ds051831.mongolab.com:51831/heroku_app34606139  

h。将 myapp 部署到 heroku

$ git push heroku master

我。在heroku上运行myapp

$ heroku open 

问题已解决:以上步骤都可以

但是我会在下面留下我的错误消息并修复该问题

应用程序显示在 localhost:3000 正常。但不是heroku

这里是从 Heroku 屏幕回复的。

The page you were looking for doesn't exist.

这是heroku日志:在这一步

$heroku 日志 --tail

2015-03-06T09:26:48.095677+00:00 heroku[web.1]: Process exited with status 143
2015-03-06T09:26:50.640199+00:00 app[web.1]: [2015-03-06 09:26:50] INFO  WEBrick 1.3.1
2015-03-06T09:26:50.640660+00:00 app[web.1]: [2015-03-06 09:26:50] INFO  WEBrick::HTTPServer#start: pid=3 port=22998
2015-03-06T09:26:50.640225+00:00 app[web.1]: [2015-03-06 09:26:50] INFO  ruby 2.0.0 (2014-11-13) [x86_64-linux]
2015-03-06T09:26:50.762782+00:00 heroku[web.1]: State changed from starting to up
2015-03-06T09:27:01.509095+00:00 heroku[router]: at=info method=GET path="/" host=kulmong.herokuapp.com request_id=4bd004f1-7b04-4cca-883d-09010aa03ee6 fwd="103.7.18.74" dyno=web.1 connect=0ms service=73ms status=404 bytes=1829

路线.rb

Rails.application.routes.draw do

end

手动创建欢迎 Controller 并查看路线 welcome_controller.rb

class WelcomeController < ApplicationController

  # GET /welcome
  def index

  end

end

在 View 的欢迎文件夹中创建了index.erb

<h1>Welcome Rails at heroku</h1>

<p>
   Welcome! Hi 
</p>

在routes.rb中设置root为welcome

Rails.application.routes.draw do
  resources :projects
  root 'welcome#index'
end

projects_controller.rb

然后git add、提交并推送到heroku WORK

唯一的问题是当我尝试脚手架时。它仍然错误,日志中没有任何线索

$ rails g scaffold Project name:String status:String
$ git add .
$ git commit -am 'scaffold Project'
$ git push heroku master
$ heroku open

转到https://myapp.herokuapp.com/projects

问题显示

projects error

heroku 日志(到此步骤)

2015-03-06T14:59:36.681043+00:00 heroku[web.1]: Process exited with status 143
2015-03-06T14:59:39.407268+00:00 app[web.1]: [2015-03-06 14:59:39] INFO  WEBrick 1.3.1
2015-03-06T14:59:39.407642+00:00 app[web.1]: [2015-03-06 14:59:39] INFO  WEBrick::HTTPServer#start: pid=3 port=16123
2015-03-06T14:59:39.407291+00:00 app[web.1]: [2015-03-06 14:59:39] INFO  ruby 2.0.0 (2014-11-13) [x86_64-linux]
2015-03-06T14:59:39.904866+00:00 heroku[web.1]: State changed from starting to up
2015-03-06T14:59:47.112131+00:00 heroku[router]: at=info method=GET path="/" host=kulmong.herokuapp.com request_id=e02ce1c1-d787-4182-a6a3-f665d4190403 fwd="103.7.18.74" dyno=web.1 connect=2ms service=26ms status=200 bytes=1411
2015-03-06T14:59:47.490918+00:00 heroku[router]: at=info method=GET path="/assets/application-3942007d31710307dd44000cb1f768c9.css" host=kulmong.herokuapp.com request_id=ad0f7b1f-9f14-4b82-9dbe-0feaf72b3e10 fwd="103.7.18.74" dyno=web.1 connect=1ms service=5ms status=200 bytes=633
2015-03-06T14:59:48.366121+00:00 heroku[router]: at=info method=GET path="/assets/application-47fe71a3b34a93e1929c175b1755d405.js" host=kulmong.herokuapp.com request_id=2e38635a-a76a-4aa4-abf7-f95fd0560716 fwd="103.7.18.74" dyno=web.1 connect=1ms service=53ms status=200 bytes=39912
2015-03-06T14:59:56.493787+00:00 heroku[router]: at=info method=GET path="/" host=kulmong.herokuapp.com request_id=5f98e00e-47a0-4054-8ced-3c4a42a2e2dd fwd="103.7.18.74" dyno=web.1 connect=1ms service=7ms status=200 bytes=1411
2015-03-06T14:59:56.936889+00:00 heroku[router]: at=info method=GET path="/assets/application-3942007d31710307dd44000cb1f768c9.css" host=kulmong.herokuapp.com request_id=a44c2289-a589-4902-acc1-93eb170a2359 fwd="103.7.18.74" dyno=web.1 connect=1ms service=5ms status=304 bytes=204
2015-03-06T15:00:01.756893+00:00 heroku[router]: at=info method=GET path="/projects" host=kulmong.herokuapp.com request_id=d2364420-3aa1-41b0-8f88-f36b51edd88b fwd="103.7.18.74" dyno=web.1 connect=5ms service=22ms status=500 bytes=1754
2015-03-06T15:46:21.167800+00:00 heroku[router]: at=info method=GET path="/" host=kulmong.herokuapp.com request_id=bbfd8332-d09d-4221-b8a0-a705dcef8c1c fwd="103.7.18.74" dyno=web.1 connect=1ms service=8ms status=200 bytes=1411
2015-03-06T15:46:21.647276+00:00 heroku[router]: at=info method=GET path="/assets/application-3942007d31710307dd44000cb1f768c9.css" host=kulmong.herokuapp.com request_id=39ecc6fb-0f39-4670-b326-2b63b53ef289 fwd="103.7.18.74" dyno=web.1 connect=2ms service=11ms status=304 bytes=204
2015-03-06T15:46:22.261151+00:00 heroku[router]: at=info method=GET path="/assets/application-47fe71a3b34a93e1929c175b1755d405.js" host=kulmong.herokuapp.com request_id=d7161d4e-b18c-434b-8fa3-b4f229f96085 fwd="103.7.18.74" dyno=web.1 connect=1ms service=4ms status=304 bytes=218
2015-03-06T15:46:34.182402+00:00 heroku[router]: at=info method=GET path="/" host=kulmong.herokuapp.com request_id=d639726f-ee06-45d4-950b-336b0c1c6168 fwd="103.7.18.74" dyno=web.1 connect=1ms service=8ms status=200 bytes=1411
2015-03-06T15:46:34.612368+00:00 heroku[router]: at=info method=GET path="/assets/application-3942007d31710307dd44000cb1f768c9.css" host=kulmong.herokuapp.com request_id=1ccababa-b634-4182-985b-6e42434123a3 fwd="103.7.18.74" dyno=web.1 connect=1ms service=4ms status=304 bytes=204
2015-03-06T15:46:35.304563+00:00 heroku[router]: at=info method=GET path="/assets/application-47fe71a3b34a93e1929c175b1755d405.js" host=kulmong.herokuapp.com request_id=33473a17-3d52-483e-88f4-556c202fbec9 fwd="103.7.18.74" dyno=web.1 connect=1ms service=4ms status=304 bytes=218
2015-03-06T15:46:36.413113+00:00 heroku[router]: at=info method=GET path="/projects" host=kulmong.herokuapp.com request_id=3046c0c7-3c96-4bcf-bc45-a197acfed716 fwd="103.7.18.74" dyno=web.1 connect=1ms service=16ms status=500 bytes=1754

routes.rb(在此步骤)

Rails.application.routes.draw do
  resources :projects

  root 'welcome#index'
end

projects_controller.rb

class ProjectsController < ApplicationController
before_action :set_project, only: [:show, :edit, :update, :destroy]

# GET /projects
# GET /projects.json
def index
  @projects = Project.all
end

# GET /projects/1
# GET /projects/1.json
def show
end

# GET /projects/new
def new
  @project = Project.new
end

# GET /projects/1/edit
def edit
end

# POST /projects
# POST /projects.json
def create
  @project = Project.new(project_params)

  respond_to do |format|
    if @project.save
      format.html { redirect_to @project, notice: 'Project was successfully created.' }
      format.json { render :show, status: :created, location: @project }
    else
      format.html { render :new }
      format.json { render json: @project.errors, status: :unprocessable_entity }
    end
  end
end

# PATCH/PUT /projects/1
# PATCH/PUT /projects/1.json
def update
  respond_to do |format|
    if @project.update(project_params)
      format.html { redirect_to @project, notice: 'Project was successfully updated.' }
      format.json { render :show, status: :ok, location: @project }
    else
      format.html { render :edit }
      format.json { render json: @project.errors, status: :unprocessable_entity }
    end
  end
end

# DELETE /projects/1
# DELETE /projects/1.json
def destroy
  @project.destroy
  respond_to do |format|
    format.html { redirect_to projects_url, notice: 'Project was successfully destroyed.' }
    format.json { head :no_content }
  end
end

private
  # Use callbacks to share common setup or constraints between actions.
  def set_project
    @project = Project.find(params[:id])
  end

  # Never trust parameters from the scary internet, only allow the white list through.
  def project_params
    params.require(:project).permit(:name, :status)
  end
end

添加 gem “rails_12factor”

group :production do
  gem 'rails_12factor'
end

从答案中添加组生成后,heroku 会记录

2015-03-06T17:09:14.952981+00:00 heroku[api]: Deploy c657570 by 1.0@kul.com
2015-03-06T17:09:14.952981+00:00 heroku[api]: Release v19 created by 1.0@kul.com
2015-03-06T17:09:15.208140+00:00 heroku[web.1]: State changed from up to starting
2015-03-06T17:09:17.078710+00:00 heroku[web.1]: Stopping all processes with SIGTERM
2015-03-06T17:09:17.466316+00:00 app[web.1]: [2015-03-06 17:09:17] FATAL SignalException: SIGTERM
2015-03-06T17:09:17.466324+00:00 app[web.1]:  /app/vendor/ruby-2.0.0/lib/ruby/2.0.0/webrick/server.rb:170:in `select'
2015-03-06T17:09:17.466327+00:00 app[web.1]:  /app/vendor/ruby-2.0.0/lib/ruby/2.0.0/webrick/server.rb:170:in `block in start'
2015-03-06T17:09:17.466329+00:00 app[web.1]:  /app/vendor/ruby-2.0.0/lib/ruby/2.0.0/webrick/server.rb:32:in `start'
2015-03-06T17:09:17.466331+00:00 app[web.1]:  /app/vendor/ruby-2.0.0/lib/ruby/2.0.0/webrick/server.rb:160:in `start'
2015-03-06T17:09:17.466333+00:00 app[web.1]:  /app/vendor/bundle/ruby/2.0.0/gems/rack-1.6.0/lib/rack/handler/webrick.rb:35:in `run'
2015-03-06T17:09:17.466335+00:00 app[web.1]:  /app/vendor/bundle/ruby/2.0.0/gems/rack-1.6.0/lib/rack/server.rb:286:in `start'
2015-03-06T17:09:17.466337+00:00 app[web.1]:  /app/vendor/bundle/ruby/2.0.0/gems/railties-4.2.0/lib/rails/commands/server.rb:80:in `start'
2015-03-06T17:09:17.466340+00:00 app[web.1]:  /app/vendor/bundle/ruby/2.0.0/gems/railties-4.2.0/lib/rails/commands/commands_tasks.rb:80:in `block in server'
2015-03-06T17:09:17.466342+00:00 app[web.1]:  /app/vendor/bundle/ruby/2.0.0/gems/railties-4.2.0/lib/rails/commands/commands_tasks.rb:75:in `tap'
2015-03-06T17:09:17.466344+00:00 app[web.1]:  /app/vendor/bundle/ruby/2.0.0/gems/railties-4.2.0/lib/rails/commands/commands_tasks.rb:75:in `server'
2015-03-06T17:09:17.466347+00:00 app[web.1]:  /app/vendor/bundle/ruby/2.0.0/gems/railties-4.2.0/lib/rails/commands/commands_tasks.rb:39:in `run_command!'
2015-03-06T17:09:17.466349+00:00 app[web.1]:  /app/vendor/bundle/ruby/2.0.0/gems/railties-4.2.0/lib/rails/commands.rb:17:in `<top (required)>'
2015-03-06T17:09:17.466352+00:00 app[web.1]:  bin/rails:8:in `require'
2015-03-06T17:09:17.466354+00:00 app[web.1]:  bin/rails:8:in `<main>'
2015-03-06T17:09:17.466364+00:00 app[web.1]: [2015-03-06 17:09:17] INFO  going to shutdown ...
2015-03-06T17:09:17.466366+00:00 app[web.1]: [2015-03-06 17:09:17] INFO  WEBrick::HTTPServer#start done.
2015-03-06T17:09:17.466389+00:00 app[web.1]: Exiting
2015-03-06T17:09:18.335257+00:00 heroku[web.1]: Process exited with status 143
2015-03-06T17:09:18.985032+00:00 heroku[web.1]: Starting process with command `bin/rails server -p 32299 -e production`
2015-03-06T17:09:22.606059+00:00 app[web.1]: [2015-03-06 17:09:22] INFO  WEBrick 1.3.1
2015-03-06T17:09:22.606082+00:00 app[web.1]: [2015-03-06 17:09:22] INFO  ruby 2.2.0 (2014-12-25) [x86_64-linux]
2015-03-06T17:09:22.606506+00:00 app[web.1]: [2015-03-06 17:09:22] INFO  WEBrick::HTTPServer#start: pid=3 port=32299
2015-03-06T17:09:22.961214+00:00 heroku[web.1]: State changed from starting to up
2015-03-06T17:10:14.517067+00:00 heroku[router]: at=info method=GET path="/" host=kulmong.herokuapp.com request_id=4bf324fc-d0de-4dfa-800e-62ee04e5e4a2 fwd="103.7.18.74" dyno=web.1 connect=1ms service=28ms status=200 bytes=1411
2015-03-06T17:10:14.487257+00:00 app[web.1]: => Booting WEBrick
2015-03-06T17:10:14.487262+00:00 app[web.1]: => Rails 4.2.0 application starting in production on http://0.0.0.0:32299
2015-03-06T17:10:14.487264+00:00 app[web.1]: => Run `rails server -h` for more startup options
2015-03-06T17:10:14.487266+00:00 app[web.1]: => Ctrl-C to shutdown server
2015-03-06T17:10:14.487267+00:00 app[web.1]: Started GET "/" for 103.7.18.74 at 2015-03-06 17:10:14 +0000
2015-03-06T17:10:14.491090+00:00 app[web.1]: Processing by WelcomeController#index as HTML
2015-03-06T17:10:14.507851+00:00 app[web.1]: Completed 200 OK in 17ms (Views: 7.5ms)
2015-03-06T17:10:14.505187+00:00 app[web.1]:   Rendered welcome/index.erb within layouts/application (0.6ms)
2015-03-06T17:10:14.922125+00:00 heroku[router]: at=info method=GET path="/assets/application-3942007d31710307dd44000cb1f768c9.css" host=kulmong.herokuapp.com request_id=58fcd9b2-3ad5-464f-ad23-660d6492ad4a fwd="103.7.18.74" dyno=web.1 connect=1ms service=5ms status=304 bytes=204
2015-03-06T17:10:15.711894+00:00 heroku[router]: at=info method=GET path="/assets/application-47fe71a3b34a93e1929c175b1755d405.js" host=kulmong.herokuapp.com request_id=62f0a025-bb63-4daf-99eb-b3c96f243654 fwd="103.7.18.74" dyno=web.1 connect=0ms service=5ms status=304 bytes=218
2015-03-06T17:10:28.116004+00:00 heroku[router]: at=info method=GET path="/projects" host=kulmong.herokuapp.com request_id=0c2c62f8-363d-478d-a82a-17ea07b65bb5 fwd="103.7.18.74" dyno=web.1 connect=1ms service=17ms status=500 bytes=1754
2015-03-06T17:10:28.104574+00:00 app[web.1]: Processing by ProjectsController#index as HTML
2015-03-06T17:10:28.102356+00:00 app[web.1]: Started GET "/projects" for 103.7.18.74 at 2015-03-06 17:10:28 +0000
2015-03-06T17:10:28.112683+00:00 app[web.1]:   Rendered projects/index.html.erb within layouts/application (2.8ms)
2015-03-06T17:10:28.112783+00:00 app[web.1]: Completed 500 Internal Server Error in 8ms
2015-03-06T17:10:28.114586+00:00 app[web.1]: 
2015-03-06T17:10:28.114589+00:00 app[web.1]: ActionView::Template::Error (:skip_version_check is an unknown option. Valid options are: :write, :read, :database, :max_retries, :pool_size, :pool_timeout, :retry_interval, :refresh_interval, :down_interval, :ssl, :timeout, :instrumenter, :auto_discover.):
2015-03-06T17:10:28.114591+00:00 app[web.1]:     12:   </thead>
2015-03-06T17:10:28.114593+00:00 app[web.1]:     13: 
2015-03-06T17:10:28.114594+00:00 app[web.1]:     14:   <tbody>
2015-03-06T17:10:28.114596+00:00 app[web.1]:     15:     <% @projects.each do |project| %>
2015-03-06T17:10:28.114597+00:00 app[web.1]:     16:       <tr>
2015-03-06T17:10:28.114599+00:00 app[web.1]:     17:         <td><%= project.name %></td>
2015-03-06T17:10:28.114600+00:00 app[web.1]:     18:         <td><%= project.status %></td>
2015-03-06T17:10:28.114602+00:00 app[web.1]:   app/views/projects/index.html.erb:15:in `_app_views_projects_index_html_erb___3371573674626367126_70074889855940'
2015-03-06T17:10:28.114604+00:00 app[web.1]: 
2015-03-06T17:10:28.114605+00:00 app[web.1]: 

谢谢

P.库尔

最佳答案

当你想在heroku上部署时,你必须在你的Gemfile中添加:

group :production do
  gem 'rails_12factor'
end

引用:https://devcenter.heroku.com/articles/getting-started-with-rails4#write-your-app

使用您的存储库进行测试后,您需要删除 mongoid.yml 上声明的选项:

production:
  sessions:
    default:
      uri: <%= ENV['MONGOLAB_URI'] %>

Gemfile 中声明 ruby​​ 版本是一个很好的做法,如下所示:

source 'https://rubygems.org'
ruby "2.1.3"
...

在我的heroku应用程序上测试它:https://tranquil-woodland-7416.herokuapp.com/projects

关于ruby-on-rails - 使用 mongolab 的 Heroku 的 mongodb.yml,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28894152/

相关文章:

ruby-on-rails - Rails 3 多模型查询

ruby-on-rails - 事件记录错误 : Couldn't find Customer with 'id' =1 [WHERE (`customers` .`company_id` IS NOT NULL) AND `customers` .`company_id` = ?] RUBY ON RAILS

ruby-on-rails - Elasticsearch清除单个查询词的缓存

django - 由于 gdal 库,heroku push 被拒绝

ruby - 批量调用将 "new"对象保存到mongoid中

ruby-on-rails - Gemfile.lock 包含合并冲突

django - git push heroku master : Heroku push rejected, 未检测到支持 Cedar 的应用程序

ruby-on-rails - Sendgrid 上的 Heroku 错误

ruby - MongoID 和一对多关系的问题

ruby-on-rails - 创建和更新 Mongoid 数组字段的表单