不能用Sinatra开始Mongoid

时间:2013-08-15 23:02:52

标签: ruby sinatra mongoid

我正在尝试在Sinatra应用程序中使用Mongoid,但看起来我遇到了依赖性问题。如果它有帮助,我会使用RVM并运行Ruby 1.9.3的Macbook Pro。

以下是运行$ ruby​​ config.ru后出现的错误:

/Users/duncanmalashock/.rvm/rubies/ruby-1.9.3-p448/lib/ruby/1.9.1/rubygems/specification.rb:1637:in `raise_if_conflicts': Unable to activate mongoid-3.0.23, because activemodel-4.0.0 conflicts with activemodel (~> 3.1) (Gem::LoadError)
from /Users/duncanmalashock/.rvm/rubies/ruby-1.9.3-p448/lib/ruby/1.9.1/rubygems/specification.rb:746:in `activate'
from /Users/duncanmalashock/.rvm/rubies/ruby-1.9.3-p448/lib/ruby/1.9.1/rubygems.rb:212:in `rescue in try_activate'
from /Users/duncanmalashock/.rvm/rubies/ruby-1.9.3-p448/lib/ruby/1.9.1/rubygems.rb:209:in `try_activate'
from /Users/duncanmalashock/.rvm/rubies/ruby-1.9.3-p448/lib/ruby/1.9.1/rubygems/custom_require.rb:59:in `rescue in require'
from /Users/duncanmalashock/.rvm/rubies/ruby-1.9.3-p448/lib/ruby/1.9.1/rubygems/custom_require.rb:35:in `require'
from /Users/duncanmalashock/Penumbra/penumbra.rb:4:in `<top (required)>'
from /Users/duncanmalashock/.rvm/rubies/ruby-1.9.3-p448/lib/ruby/1.9.1/rubygems/custom_require.rb:36:in `require'
from /Users/duncanmalashock/.rvm/rubies/ruby-1.9.3-p448/lib/ruby/1.9.1/rubygems/custom_require.rb:36:in `require'
from config.ru:2:in `<main>'

有人可以帮忙吗?

以下是涉及的文件:

mongoid.yml:

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

config.ru:

require 'sinatra'
require './penumbra'
run Sinatra::Application

的Gemfile:

source :rubygems
source :rubyforge

# Thin Server
gem 'thin'

# Sinatra
gem 'sinatra'
gem 'sinatra-contrib', :require => 'sinatra/multi_route'
gem 'sinatra-partial', :require => 'sinatra/partial'
gem 'sinatra-reloader'

# MongoDB
gem "mongoid"

# Shopify
gem 'shopify_api'

penumbra.rb:

require 'sinatra'
require 'shopify_api'
require 'mongo'
require 'mongoid'

require 'json'
require "sinatra/reloader" if development?

Mongoid.load!("./mongoid.yml", :production)

def isactivepage(link_name)
    if (link_name == @page_name)
        return ' activelink'
    else return ''
    end
end

get '/' do
    @page_name = "Home"
    erb :"pages/index"
end

get '/about' do
    @page_name = "About"
    erb :"pages/about"
end

运行$ bundle exec rackup后:

/Users/duncanmalashock/.rvm/gems/ruby-1.9.3-p448/gems/mongoid-1.0.6/lib/mongoid.rb:68:in `method_missing': undefined method `load!' for #<Mongoid::Config:0x007fc0838cb1d0> (NoMethodError)
from /Users/duncanmalashock/Penumbra/penumbra.rb:9:in `<top (required)>'
from /Users/duncanmalashock/Penumbra/config.ru:2:in `require'
from /Users/duncanmalashock/Penumbra/config.ru:2:in `block in <main>'
from /Users/duncanmalashock/.rvm/gems/ruby-1.9.3-p448/gems/rack-1.5.2/lib/rack/builder.rb:55:in `instance_eval'
from /Users/duncanmalashock/.rvm/gems/ruby-1.9.3-p448/gems/rack-1.5.2/lib/rack/builder.rb:55:in `initialize'
from /Users/duncanmalashock/Penumbra/config.ru:in `new'
from /Users/duncanmalashock/Penumbra/config.ru:in `<main>'
from /Users/duncanmalashock/.rvm/gems/ruby-1.9.3-p448/gems/rack-1.5.2/lib/rack/builder.rb:49:in `eval'
from /Users/duncanmalashock/.rvm/gems/ruby-1.9.3-p448/gems/rack-1.5.2/lib/rack/builder.rb:49:in `new_from_string'
from /Users/duncanmalashock/.rvm/gems/ruby-1.9.3-p448/gems/rack-1.5.2/lib/rack/builder.rb:40:in `parse_file'
from /Users/duncanmalashock/.rvm/gems/ruby-1.9.3-p448/gems/rack-1.5.2/lib/rack/server.rb:277:in `build_app_and_options_from_config'
from /Users/duncanmalashock/.rvm/gems/ruby-1.9.3-p448/gems/rack-1.5.2/lib/rack/server.rb:199:in `app'
from /Users/duncanmalashock/.rvm/gems/ruby-1.9.3-p448/gems/rack-1.5.2/lib/rack/server.rb:314:in `wrapped_app'
from /Users/duncanmalashock/.rvm/gems/ruby-1.9.3-p448/gems/rack-1.5.2/lib/rack/server.rb:250:in `start'
from /Users/duncanmalashock/.rvm/gems/ruby-1.9.3-p448/gems/rack-1.5.2/lib/rack/server.rb:141:in `start'
from /Users/duncanmalashock/.rvm/gems/ruby-1.9.3-p448/gems/rack-1.5.2/bin/rackup:4:in `<top (required)>'
from /Users/duncanmalashock/.rvm/gems/ruby-1.9.3-p448/bin/rackup:23:in `load'
from /Users/duncanmalashock/.rvm/gems/ruby-1.9.3-p448/bin/rackup:23:in `<main>'

1 个答案:

答案 0 :(得分:3)

您有一个Gemfile,但是您没有使用Bundler启动您的应用,这就是您遇到冲突的原因。

当前版本的shopify_api(3.0.3)依赖于activemodel通过activeresourceversion requirements set to >=3.0.0。在您的系统上安装了Rails 4,因此加载了activemodel 4.0。

当前版本的mongoid也依赖于activemodel,但这次是version requirement is ~> 3.2

这两个版本要求不兼容,因此您收到错误activemodel-4.0.0 conflicts with activemodel (~> 3.1)

要解决此问题,只需使用Bundler启动您的应用即可。您说您使用的是$ ruby config.ru,但这不是使用config.ru的正确方法(即使您修复了依赖项问题,也会在以后遇到错误)。您应该使用rackup。在这种情况下,您应该运行:

$ bundle exec rackup