载波上传器无法访问会话

时间:2016-04-05 06:27:12

标签: ruby-on-rails carrierwave spree

class AvatarUploader < CarrierWave::Uploader::Base

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

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

   storage :fog

  # Override the directory where uploaded files will be stored.
  # This is a sensible default for uploaders that are meant to be mounted:
  def store_dir
    byebug
    session[:user_email_id]

  end

  def get_time
    @time = Time.now
  end

  # Provide a default URL as a default if there hasn't been a file uploaded:
  # 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

  # Process files as they are uploaded:
  # process scale: [200, 300]
  #
  # def scale(width, height)
  #   # do something
  # end

  # Create different versions of your uploaded files:
  # version :thumb do
  #   process resize_to_fit: [50, 50]
  # end

  # Add a white list of extensions which are allowed to be uploaded.
  # For images you might use something like this:
   def extension_whitelist
     %w(jpg jpeg gif png)
   end

  # Override the filename of the uploaded files:
  # Avoid using model.id or version_name here, see uploader/store.rb for details.
  # def filename
  #   "something.jpg" if original_filename
  # end

end

在上面的store_dir方法中,它需要返回一个字符串。所以我正在尝试将字符串作为当前用户的电子邮件ID。已在其他控制器中设置,并且可在其他控制器中访问。我有什么遗漏与载波有关吗?

此AvatarUploader无法访问spree_current_user

1 个答案:

答案 0 :(得分:0)

当我们在控制器中使用“session [xx]”时,因为控制器有一个名为“session”的方法,它返回用户的会话。我相信AvatarUploader或CarrierWave :: Uploader :: Base都没有这种会话方法,这就是为什么你不能在其中调用'session [xxx]'。