在Sinatra上开始Mongoid的问题

时间:2014-02-08 08:09:19

标签: ruby mongodb sinatra mongoid unicorn

我正在尝试使用Unicorn + Mongoid在Vagrant上运行Sinatra应用程序。 这是一个简单的Hello world代码,除了我在这里尝试将MongoDB部署到它中。 以下是一些代码:

models.rb

class Chat
    include Mongoid::Document
    field :author, type: String
    field :text, type: String
end

main.rb的

require "rubygems"
require "sinatra"
require "logger"
require "unicorn"
require "mongoid"
require_relative './models.rb'

Mongoid.load!("/vagrant/workspace/config/mongoid.yml", :development)

class MainApp < Sinatra::Base
    get '/' do
        "Hello World"
    end
    get '/chat/:field' do
        content_type :author
        hito = Chat.first
        hito ? "#{hito.__send__ params[:field]}" : "nil"
    end
end

的Gemfile

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

gem 'unicorn'
gem 'sinatra',require: 'sinatra/base'
gem 'slim'
gem 'mongoid'
gem 'mongo'
gem 'bson_ext'

mongoid.yml

development:
    sessions:
        default:
            database: mydb
                hosts:
                - localhost:27017

当我尝试:

时,我收到了以下错误
bundle exec unicorn -c ./unicorn.rb -D
stderr日志说:

/home/vagrant/.rvm/rubies/ruby-2.1.0/lib/ruby/2.1.0/psych.rb:369:in `parse':     (<unknown>): mapping values are not allowed in this context at line 5 colum    n 22 (Psych::SyntaxError)
from /home/vagrant/.rvm/rubies/ruby-2.1.0/lib/ruby/2.1.0/psych.rb:369:in `parse_stream'
from /home/vagrant/.rvm/rubies/ruby-2.1.0/lib/ruby/2.1.0/psych.rb:317:in `parse'
from /home/vagrant/.rvm/rubies/ruby-2.1.0/lib/ruby/2.1.0/psych.rb:244:in `load'
from /home/vagrant/.rvm/gems/ruby-2.1.0/gems/mongoid-3.1.6/lib/mongoid/config/environment.rb:40:in `load_yaml'
from /home/vagrant/.rvm/gems/ruby-2.1.0/gems/mongoid-3.1.6/lib/mongoid/config.rb:89:in `load!'
from /home/vagrant/.rvm/gems/ruby-2.1.0/gems/mongoid-3.1.6/lib/mongoid.rb:163:in `load!'
from /vagrant/workspace/lib/main.rb:8:in `<top (required)>'
from config.ru:3:in `require'
from config.ru:3:in `block in <main>'
from /home/vagrant/.rvm/gems/ruby-2.1.0/gems/rack-1.5.2/lib/rack/builder.rb:55:in `instance_eval'
from /home/vagrant/.rvm/gems/ruby-2.1.0/gems/rack-1.5.2/lib/rack/builder.rb:55:in `initialize'
from config.ru:1:in `new'
from config.ru:1:in `<main>'
from /home/vagrant/.rvm/gems/ruby-2.1.0/gems/unicorn-4.8.2/lib/unicorn.rb:48:in `eval'
from /home/vagrant/.rvm/gems/ruby-2.1.0/gems/unicorn-4.8.2/lib/unicorn.rb:48:in `block in builder'
from /home/vagrant/.rvm/gems/ruby-2.1.0/gems/unicorn-4.8.2/lib/unicorn/http_server.rb:760:in `call'
from /home/vagrant/.rvm/gems/ruby-2.1.0/gems/unicorn-4.8.2/lib/unicorn/http_server.rb:760:in `build_app!'
from /home/vagrant/.rvm/gems/ruby-2.1.0/gems/unicorn-4.8.2/lib/unicorn/http_server.rb:137:in `start'
from /home/vagrant/.rvm/gems/ruby-2.1.0/gems/unicorn-4.8.2/bin/unicorn:126:in `<top (required)>'
from /home/vagrant/.rvm/gems/ruby-2.1.0/bin/unicorn:23:in `load'
from /home/vagrant/.rvm/gems/ruby-2.1.0/bin/unicorn:23:in `<main>'
from /home/vagrant/.rvm/gems/ruby-2.1.0/bin/ruby_executable_hooks:15:in `eval'
from /home/vagrant/.rvm/gems/ruby-2.1.0/bin/ruby_executable_hooks:15:in `<main>'

任何提示都会非常有用。

config.ru

require 'sinatra'
require './lib/main.rb'
run Sinatra::Application

0 个答案:

没有答案