0
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和paperclip。我試圖根據Photo.current_account的值設置圖像的路徑,但它似乎不起作用。但是,當我把它放在rename_image中時,它就可以工作。什麼可能是錯的?我對此進行了設置,以便在用戶上傳時,目標將基於該請求期間用戶的子域。 Photo.current_account已經擁有控制器的子域信息。無法使用以下設置自定義回形針路徑:url

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

目前,輸出就像上面的行一樣,你可以在系統/它的空白字符串之後看到。

回答

0

我得到了它的使用回形針插在當前模型添加下面的代碼工作。

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

然後使用

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