【问题标题】:Rails Carrierwave stor_dir location not valid (Model.#{association} returns nil)Rails Carrierwave stor_dir 位置无效(Model.#{association} 返回 nil)
【发布时间】:2013-04-01 18:17:16
【问题描述】:

我正在使用carrierwave将图像上传到我的网络应用程序。

有必要将它们上传到父模型的位置。

即。

父母是有很多图像的房子。

所以我想将图像存储在

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

我如何获得正确的路径/我做错了什么:(

【问题讨论】:

    标签: ruby-on-rails ruby image upload carrierwave


    【解决方案1】:

    请尝试:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-04-10
      相关资源
      最近更新 更多