2013-04-10 14 views
0

我正在使用carrierwave將圖像上傳到我的webapp。Rails Carrierwave stor_dir位置無效(型號#{關聯}返回零)

已經有必要將它們上傳到父模型的位置。

即,

父母是有許多圖像的房子。

所以我想將圖像存儲在

public/uploads/houses/images/[:house_id]/ 

這是我的當前設置。

..uploaders/image_uploader.rb 

    def store_dir 
    puts "uploads/house/#{model.house_id}/#{mounted_as}/#{model.id}" 
    "uploads/house/#{model.house_id}/#{mounted_as}/#{model.id}" 
    end 

puts語句打印出我想要的正確路徑,但保存的路徑不匹配。 看來,model.house_id被返回nil

房屋模型

class House < ActiveRecord::Base 
    attr_accessible :address, :description, :title, :price, :image, :image_id, :images, :image_cache 
    has_many :images 
    mount_uploader :image, ImageUploader 
end 

圖像模型

class Image < ActiveRecord::Base 
    attr_accessible :house_id, :image 
    mount_uploader :image, ImageUploader 
    belongs_to :house 
end 

如何獲取正確的路徑/我在做什麼錯:(

回答

0

請嘗試:

def cache_dir 
"#{Rails.root}/public/uploads/houses/images" 
end 
def store_dir 
    "#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}" 
end