从本地文件上传时出错:未定义的方法`type_for' for MIME :: Types:Class

时间:2016-05-19 00:58:54

标签: ruby-on-rails carrierwave

我正在创建一个使用Carrierwave 1.2.1的Rails 4应用程序。我遇到的问题是当我运行rake db:reset时。一切都很好,直到播种数据库。错误是:



rake aborted!
NoMethodError: undefined method `type_for' for MIME::Types:Class
/Users/MASTER/.bundler/ruby/2.2.0/bundler/gems/carrierwave-b31f7ce006ba/lib/carrierwave/sanitized_file.rb:271:in `content_type'
/Users/MASTER/.bundler/ruby/2.2.0/bundler/gems/carrierwave-b31f7ce006ba/lib/carrierwave/sanitized_file.rb:226:in `copy_to'
/Users/MASTER/.bundler/ruby/2.2.0/bundler/gems/carrierwave-b31f7ce006ba/lib/carrierwave/uploader/cache.rb:134:in `cache!'
/Users/MASTER/.bundler/ruby/2.2.0/bundler/gems/carrierwave-b31f7ce006ba/lib/carrierwave/mounter.rb:43:in `block in cache'
/Users/MASTER/.bundler/ruby/2.2.0/bundler/gems/carrierwave-b31f7ce006ba/lib/carrierwave/mounter.rb:41:in `map'
/Users/MASTER/.bundler/ruby/2.2.0/bundler/gems/carrierwave-b31f7ce006ba/lib/carrierwave/mounter.rb:41:in `cache'
/Users/MASTER/.bundler/ruby/2.2.0/bundler/gems/carrierwave-b31f7ce006ba/lib/carrierwave/mount.rb:146:in `lender_document_file='
/Users/MASTER/.bundler/ruby/2.2.0/bundler/gems/carrierwave-b31f7ce006ba/lib/carrierwave/orm/activerecord.rb:70:in `lender_document_file='
(APP_PATH)/db/seeds.rb:92:in `<top (required)>'
/Users/MASTER/.bundler/ruby/2.2.0/gems/activesupport-4.1.11/lib/active_support/dependencies.rb:241:in `load'
/Users/MASTER/.bundler/ruby/2.2.0/gems/activesupport-4.1.11/lib/active_support/dependencies.rb:241:in `block in load'
/Users/MASTER/.bundler/ruby/2.2.0/gems/activesupport-4.1.11/lib/active_support/dependencies.rb:232:in `load_dependency'
/Users/MASTER/.bundler/ruby/2.2.0/gems/activesupport-4.1.11/lib/active_support/dependencies.rb:241:in `load'
/Users/MASTER/.bundler/ruby/2.2.0/gems/railties-4.1.11/lib/rails/engine.rb:543:in `load_seed'
/Users/MASTER/.bundler/ruby/2.2.0/gems/activerecord-4.1.11/lib/active_record/tasks/database_tasks.rb:198:in `load_seed'
/Users/MASTER/.bundler/ruby/2.2.0/gems/activerecord-4.1.11/lib/active_record/railties/databases.rake:174:in `block (2 levels) in <top (required)>'
/Users/MASTER/.bundler/ruby/2.2.0/gems/activerecord-4.1.11/lib/active_record/railties/databases.rake:133:in `block (2 levels) in <top (required)>'
/Users/MASTER/.rvm/gems/ruby-2.2.1/bin/ruby_executable_hooks:15:in `eval'
/Users/MASTER/.rvm/gems/ruby-2.2.1/bin/ruby_executable_hooks:15:in `<main>'
Tasks: TOP => db:setup => db:seed
(See full trace by running task with --trace)
&#13;
&#13;
&#13;

种子文件在指定的行(92)上有这个:

@lender.lender_document_file = File.open("#{Rails.root}/app/assets/images/afra.png")

Lender模型有:mount_uploader :lender_document_file, ReceiptUploader

这是ReceiptUploader

&#13;
&#13;
# encoding: utf-8

class ReceiptUploader < CarrierWave::Uploader::Base

  # Choose what kind of storage to use for this uploader:
  if Rails.env.test? or Rails.env.cucumber? or Rails.env.development? or Rails.env.demo?
    storage :file

    def store_dir
      "uploads_marketplace/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
    end
  else

    storage :fog

    def store_dir
      "uploads_marketplace/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
    end
  end

  def default_url
    #   # For Rails 3.1+ asset pipeline compatibility:
    ActionController::Base.helpers.asset_path("fallback/" + [version_name, "default.png"].compact.join('_'))
    #
    #   "/images/fallback/" + [version_name, "default.png"].compact.join('_')
  end

end
&#13;
&#13;
&#13;

我使用的是Carrierwave版本1.2.1,mime-types(3.0,2.99.1,2.99,2.6.2)和mime-types-data(3.2016.0221)。

1 个答案:

答案 0 :(得分:0)

明确地将gem 'mime-types'添加到我的Gemfile修复了此问题。 我基于这个问题:Cannot add inline attachment with actionmailer in rails 3