【发布时间】:2015-12-08 19:42:21
【问题描述】:
我有一个 ThreesixtyViewer 模型,它也有一个 ThreesixtyViewerImage 模型的嵌套资源。正在通过 paperclip gem 保存图像属性 - 但我在更新文件路径时遇到问题。
每个 ThreesixtyViewer 的图像需要一起保存在与特定查看器关联的一个目录中。例如:
/public/system/threesixty_viewer_images/12/large/filename.jpg
在此示例中,路径中的 12 将是特定 threesixtyviewer 的 id - 但我找不到任何具有该功能的示例。如果 ThreesixtyViewer 的 ID 为 57,则路径如下所示:
/public/system/threesixty_viewer_images/57/large/filename.jpg
threesixty_viewer.rb
belongs_to :article
has_many :threesixty_viewer_images, dependent: :delete_all
accepts_nested_attributes_for :threesixty_viewer_images, allow_destroy: true
threesixty_viewer_image.rb
belongs_to :threesixty_viewer
has_attached_file :image, styles: { small: "500x500#", large: "1440x800>" },
path: ':rails_root/public/system/:class/:VIEWER_ID/:size/:filename',
url: '/system/:class/:VIEWER_ID/:size/:filename'
validates_attachment_content_type :image, content_type: /\Aimage\/.*\Z/
我知道 threesixty_viewer_image.rb 中的 has_attached_file 的 :path 和 :url 属性需要更新 - 但我不确定如何获取 threesixty_viewer 的 id...现在我在它的位置添加了一个 :VIEWER_ID。
任何帮助将不胜感激!提前感谢任何可以提供帮助的人!
【问题讨论】:
标签: ruby-on-rails ruby ruby-on-rails-4 paperclip