【问题标题】:Carrierwave - undefined method `mount_uploaders'Carrierwave - 未定义的方法“mount_uploaders”
【发布时间】:2014-12-13 03:44:50
【问题描述】:

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

当我尝试挂载上传器时出现以下错误。

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

基于此,我不明白为什么它没有加载,因为我已按照此处的自述文件中的说明进行操作 - 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>'

任何帮助都会很棒!我已经坚持了几个小时。 :-)

谢谢!

【问题讨论】:

    标签: ruby-on-rails ruby activerecord carrierwave


    【解决方案1】:

    原来问题在于 gem 中不存在该方法。

    我重新安装了 gem,但没有成功。然后我手动引用了 github master 分支,重新安装,它工作了。

    【讨论】:

    • 如何解决? “我手动引用了 github master 分支”是什么意思?你不妨直接说“我用东西修好了”。
    【解决方案2】:

    我认为您必须按照here 的建议手动要求扩展。可以试试吗?

    【讨论】:

    • 我在模型中需要它,但运气不佳。你认为我可以在哪里尝试包含它?
    • 我试图实现这一点但没有成功。在github.com/carrierwaveuploader/carrierwave/issues/1103 中有一条说明,rails 不需要这样做。但我已经在我的environemnts.rb 文件和application.rb 中尝试过,但没有运气。
    猜你喜欢
    • 2016-01-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多