carrierwave .store方法抛出错误未知的regexp选项@ model = nil @ mounted_as = nil

时间:2014-04-29 23:54:10

标签: ruby-on-rails

我有一个品牌型号

class Brand < ActiveRecord::Base
  attr_accessible :activities, :attendees, :date, :description, :name, :place, :requirements_on_event, :requirements_other, :requirements_post_event, :requirements_pre_event, :target_students, :target_universities, :type, :image
  mount_uploader :image, ImageUploader
end

我的品牌表中有以下内容

t.string   "image"

这是我的image_uploader.rb

# encoding: utf-8

class ImageUploader < CarrierWave::Uploader::Base

  # Include RMagick or MiniMagick support:
  # include CarrierWave::MiniMagick
   include CarrierWave::RMagick
  # include ::CarrierWave::Backgrounder::Delay

  # Choose what kind of storage to use for this uploader:
  storage :file
  #storage :fog

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

   def extension_white_list
     %w(jpg jpeg gif png)
   end

end

我在我的gemfile中有这些

#Picture Upload and Storage
gem 'carrierwave'
gem 'carrierwave_backgrounder'
gem 'fog'
#gem 'aws-s3'
gem 'rmagick'
gem 'mini_magick

现在问题出在carrierwave的github页面上的文档

我试试

bundle exec rails crails c

uploader = ImageUploader.new

我收到绿色的

返回的消息
=> #<ImageUploader:0x007fcf2450bf68 @model=nil, @mounted_as=nil>

为什么是@model=nil@mounted_as=nil

当我尝试这个

uploader.store!(/Users/judyngai/Desktop/brandspictures/circle_accupass.png)

我收到此错误

SyntaxError: (eval):2: unknown regexp options - jdyga

如果我试试这个

uploader.store!('/Users/judyngai/Desktop/brandspictures/circle_accupass.png')

carrierwave不会让我这样做

CarrierWave::FormNotMultipart: You tried to assign a String or a Pathname to an uploader, for security reasons, this is not allowed.

我觉得我正确安装了一切。它的轨道3.2.13应用程序。我评论了我的carrierwave.rb,因为我遇到了雾和aws的问题。

我只是尝试将此添加到我的模型中,但仍然得到同样的东西。

require 'carrierwave/orm/activerecord'

1 个答案:

答案 0 :(得分:1)

您应该按以下方式调用store!方法:

uploader.store!(File.open('/Users/judyngai/Desktop/brandspictures/circle_accupass.png'))

您应该传递File而不是String的实例。因此,错误You tried to assign a String or a Pathname to an uploader, for security reasons, this is not allowed.