ruby-on-rails - Carrierwave - 未定义方法 `mount_uploaders'

标签 ruby-on-rails ruby activerecord carrierwave

目标 => 让多文件 uploader 工作。

当我尝试安装 uploader 时出现以下错误。

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

基于此,我认为我应该确认类(class)是正确的。这是我能做的:

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>'

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

谢谢!

最佳答案

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

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

关于ruby-on-rails - Carrierwave - 未定义方法 `mount_uploaders',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27455338/

相关文章:

mysql - 我如何提高这些查询的性能?

ruby-on-rails - Rails 将具有浮点值的字符串转换为整数

ruby - 我如何确定方法选项的范围?

php - Yii-Active Record 以字符串形式返回特定列的平均结果

ruby-on-rails - 数据库/模型夹具和原始状态

ruby-on-rails - Heroku 创建未知协议(protocol) SSL 错误

mysql - 我如何将 Rails 用户身份验证/授权与 MySQL 数据库服务器中的现有权限相关联?

ruby - Ruby 中的 Object.class 和 Object.class.inspect 有什么区别?

ruby-on-rails - 在 Rails 4 中更新属性

ruby-on-rails - 如何在 activerecord 之外创建 activerecord 样式验证?