【问题标题】:Can't set custom paperclip path using :url无法使用 :url 设置自定义回形针路径
【发布时间】:2015-02-01 20:26:21
【问题描述】:
class Photo < ActiveRecord::Base
  cattr_accessor  :current_account
  attr_accessible :post_id
  attr_accessible :image

  belongs_to :post

  has_attached_file :image,
                    :styles => { :medium => ["500x375>", :jpg], :thumb => ["70x70#", :jpg] },
                    :processors => [:thumbnail, :compression],                    
                    :url => "/system/#{Photo.current_account}/:class/:attachment/:id_partition/:style/:filename",
                    :path => ':rails_root/public:url'
  validates_attachment_content_type :image, :content_type => /\Aimage\/.*\Z/
  validates_attachment_file_name :image, :matches => [/png\Z/, /jpe?g\Z/]
  validates_attachment_size :image, :less_than => 7.megabytes

  before_post_process :rename_image

  def rename_image
    puts "== Photo.current_account: #{Photo.current_account} == "
    extension = File.extname(image_file_name).downcase
    self.image.instance_write :file_name, "#{Time.zone.now.to_i.to_s}#{extension}"
  end

end

我在多租户应用程序上使用 rails 3 和回形针。我正在尝试根据 Photo.current_account 的值设置图像的路径,但它似乎不起作用。但是当我把它放在 rename_image 中时它可以工作。有什么问题?我对此进行了设置,以便在用户上传时,目标将基于该请求期间用户的子域。 Photo.current_account 已经拥有来自控制器的子域信息。

http://subdomain1.lvh.me:3000/system//photos/images/000/000/708/original/1417662848.jpg?1417662848

目前,输出类似于上面的行,您可以在 system/ 之后看到它是一个空白字符串。

【问题讨论】:

    标签: ruby-on-rails ruby ruby-on-rails-3 ruby-on-rails-3.2 paperclip


    【解决方案1】:

    我通过在当前模型中添加以下代码,使用回形针插值使其工作。

    Paperclip.interpolates :current_acct do |attachment, style|
      Photo.current_account
    end
    

    后来用

     :url => "/system/:current_acct/:class/:attachment/:id_partition/:style/:filename" 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-10-27
      • 2018-08-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多