Rails 3 runner没有检测到JSON gem?

时间:2011-01-22 18:01:56

标签: ruby json ruby-on-rails-3 gem runner

我正在尝试使用 Rails 3 环境创建 Ruby 脚本。

但是,每当我使用 Rails Runner 运行此rubyscript时,我都会收到此错误:

require 'rubygems'
require 'json'

@payload = {
"message" => "helloworld",
"station" => {"id"=>12345}
}.to_json

puts @payload

错误:

/usr/lib/ruby/gems/1.8/gems/activesupport-3.0.0.beta4/lib/active_support/dependencies.rb:212:in `require': no such file to load -- json (LoadError)
 from /usr/lib/ruby/gems/1.8/gems/activesupport-3.0.0.beta4/lib/active_support/dependencies.rb:212:in `require'
 from /usr/lib/ruby/gems/1.8/gems/activesupport-3.0.0.beta4/lib/active_support/dependencies.rb:198:in `load_dependency'
 from /usr/lib/ruby/gems/1.8/gems/activesupport-3.0.0.beta4/lib/active_support/dependencies.rb:554:in `new_constants_in'
 from /usr/lib/ruby/gems/1.8/gems/activesupport-3.0.0.beta4/lib/active_support/dependencies.rb:198:in `load_dependency'
 from /usr/lib/ruby/gems/1.8/gems/activesupport-3.0.0.beta4/lib/active_support/dependencies.rb:212:in `require'
 from createflags.rb:2
 from /usr/lib/ruby/gems/1.8/gems/railties-3.0.0.beta4/lib/rails/commands.rb:39:in `eval'
 from /usr/lib/ruby/gems/1.8/gems/railties-3.0.0.beta4/lib/rails/commands/runner.rb:47
 from /usr/lib/ruby/gems/1.8/gems/railties-3.0.0.beta4/lib/rails/commands.rb:39:in `require'
 from /usr/lib/ruby/gems/1.8/gems/railties-3.0.0.beta4/lib/rails/commands.rb:39
 from script/rails:6:in `require'
 from script/rails:6

有人可以告诉我,我可以做些什么来解决这个问题?

2 个答案:

答案 0 :(得分:2)

我并不完全熟悉Bundler的内部结构,但它可能不允许require其他gem不在Gemfile中。也许添加gem 'json'可以解决这个问题。

当然,ActiveSupport有自己的JSON模块,因此不需要引入另一个gem。

答案 1 :(得分:0)

删除需要“json”然后尝试。我相信ActiveSupport已经内置了to_json。