Carrierwave - 未定义的方法`mount_uploaders'

时间:2014-12-13 03:44:50

标签: ruby-on-rails ruby activerecord carrierwave

目标=>让多文件上传器工作。

当我尝试安装上传程序时出现以下错误。

4.1.7@2.1.3 (Message)> mount_uploaders :attachments, AttachmentUploader
NoMethodError: undefined method `mount_uploaders' for #<Class:0x007fc17b4cc658>
from /Users/mm/.rbenv/versions/2.1.3/lib/ruby/gems/2.1.0/gems/activerecord-4.1.7/lib/active_record/dynamic_matchers.rb:26:in `method_missing'

如果我删除了上传者的s,我会得到:

4.1.7@2.1.3 (Message)> mount_uploader :attachments, AttachmentUploader
=> :serializable_hash
基于此我认为我应该确认课程是正确的。这就是我能做到的:

4.1.7@2.1.3 (Message)> AttachmentUploader
=> AttachmentUploader < CarrierWave::Uploader::Base

我的模特是:

class Message < ActiveRecord::Base
  belongs_to :user
  belongs_to :conversation
  delegate :list, to: :conversation, allow_nil: true
  mount_uploaders :attachments, AttachmentUploader
end

我的AttachmentUploader课程是:

class AttachmentUploader < CarrierWave::Uploader::Base
  storage :file
end

在数据库中,我的messages表是:

create_table "messages", force: true do |t|
  t.integer  "user_id"
  t.integer  "conversation_id"
  t.datetime "created_at"
  t.datetime "updated_at"
  t.text     "payload"
  t.hstore   "headers",         default: {}, null: false
  t.hstore   "to",              default: [],              array: true
  t.hstore   "from",            default: {}, null: false
  t.string   "subject"
  t.hstore   "cc",              default: [],              array: true
  t.text     "body"
  t.text     "raw_body"
  t.text     "raw_html"
  t.json     "attachments"
end

基于此,我无法理解为什么它没有加载,因为我按照README中的说明进行了操作 - https://github.com/carrierwaveuploader/carrierwave#multiple-file-uploads

如果我启动rspec,这是一个完整的堆栈跟踪:

➜  GroupMailer git:(add_attachments) ✗ rspec
Coverage report generated for RSpec to /Users/matthewbarram/projects/GroupMailer/coverage. 48 / 100 LOC (48.0%) covered.
/Users/matthewbarram/.rbenv/versions/2.1.3/lib/ruby/gems/2.1.0/gems/activerecord-4.1.7/lib/active_record/dynamic_matchers.rb:26:in `method_missing': undefined method `mount_uploaders' for #<Class:0x007f9269adfae0> (NoMethodError)
  from /Users/matthewbarram/projects/GroupMailer/app/models/message.rb:5:in `<class:Message>'
  from /Users/matthewbarram/projects/GroupMailer/app/models/message.rb:1:in `<top (required)>'
  from /Users/matthewbarram/.rbenv/versions/2.1.3/lib/ruby/gems/2.1.0/gems/activesupport-4.1.7/lib/active_support/dependencies.rb:247:in `require'
  from /Users/matthewbarram/.rbenv/versions/2.1.3/lib/ruby/gems/2.1.0/gems/activesupport-4.1.7/lib/active_support/dependencies.rb:247:in `block in require'
  from /Users/matthewbarram/.rbenv/versions/2.1.3/lib/ruby/gems/2.1.0/gems/activesupport-4.1.7/lib/active_support/dependencies.rb:232:in `load_dependency'
  from /Users/matthewbarram/.rbenv/versions/2.1.3/lib/ruby/gems/2.1.0/gems/activesupport-4.1.7/lib/active_support/dependencies.rb:247:in `require'
  from /Users/matthewbarram/.rbenv/versions/2.1.3/lib/ruby/gems/2.1.0/gems/activesupport-4.1.7/lib/active_support/dependencies.rb:348:in `require_or_load'
  from /Users/matthewbarram/.rbenv/versions/2.1.3/lib/ruby/gems/2.1.0/gems/activesupport-4.1.7/lib/active_support/dependencies.rb:480:in `load_missing_constant'
  from /Users/matthewbarram/.rbenv/versions/2.1.3/lib/ruby/gems/2.1.0/gems/activesupport-4.1.7/lib/active_support/dependencies.rb:180:in `const_missing'
  from /Users/matthewbarram/projects/GroupMailer/spec/models/message_spec.rb:3:in `<top (required)>'
  from /Users/matthewbarram/.rbenv/versions/2.1.3/lib/ruby/gems/2.1.0/gems/activesupport-4.1.7/lib/active_support/dependencies.rb:241:in `load'
  from /Users/matthewbarram/.rbenv/versions/2.1.3/lib/ruby/gems/2.1.0/gems/activesupport-4.1.7/lib/active_support/dependencies.rb:241:in `block in load'
  from /Users/matthewbarram/.rbenv/versions/2.1.3/lib/ruby/gems/2.1.0/gems/activesupport-4.1.7/lib/active_support/dependencies.rb:232:in `load_dependency'
  from /Users/matthewbarram/.rbenv/versions/2.1.3/lib/ruby/gems/2.1.0/gems/activesupport-4.1.7/lib/active_support/dependencies.rb:241:in `load'
  from /Users/matthewbarram/.rbenv/versions/2.1.3/lib/ruby/gems/2.1.0/gems/rspec-core-3.1.7/lib/rspec/core/configuration.rb:1105:in `block in load_spec_files'
  from /Users/matthewbarram/.rbenv/versions/2.1.3/lib/ruby/gems/2.1.0/gems/rspec-core-3.1.7/lib/rspec/core/configuration.rb:1105:in `each'
  from /Users/matthewbarram/.rbenv/versions/2.1.3/lib/ruby/gems/2.1.0/gems/rspec-core-3.1.7/lib/rspec/core/configuration.rb:1105:in `load_spec_files'
  from /Users/matthewbarram/.rbenv/versions/2.1.3/lib/ruby/gems/2.1.0/gems/rspec-core-3.1.7/lib/rspec/core/runner.rb:96:in `setup'
  from /Users/matthewbarram/.rbenv/versions/2.1.3/lib/ruby/gems/2.1.0/gems/rspec-core-3.1.7/lib/rspec/core/runner.rb:84:in `run'
  from /Users/matthewbarram/.rbenv/versions/2.1.3/lib/ruby/gems/2.1.0/gems/rspec-core-3.1.7/lib/rspec/core/runner.rb:69:in `run'
  from /Users/matthewbarram/.rbenv/versions/2.1.3/lib/ruby/gems/2.1.0/gems/rspec-core-3.1.7/lib/rspec/core/runner.rb:37:in `invoke'
  from /Users/matthewbarram/.rbenv/versions/2.1.3/lib/ruby/gems/2.1.0/gems/rspec-core-3.1.7/exe/rspec:4:in `<top (required)>'
  from /Users/matthewbarram/.rbenv/versions/2.1.3/bin/rspec:23:in `load'
  from /Users/matthewbarram/.rbenv/versions/2.1.3/bin/rspec:23:in `<main>'

任何帮助都会很棒!我被困在这几个小时。 : - )

谢谢!

2 个答案:

答案 0 :(得分:8)

事实证明,问题是gem中不存在该方法。

我没有成功重新安装宝石。然后我手动引用了github master分支,重新安装并且工作正常。

答案 1 :(得分:0)

我认为你必须按照建议here手动要求扩展。你能试试吗?