2015-12-08 46 views
4

我有ThreesixtyViewer也有ThreesixtyViewerImage模型嵌套資源的典範。圖像屬性正在通過paperclip gem保存 - 但我在更新文件路徑時需要如何解決問題。Rails的回形針寶石 - 父模型ID保存路徑

需要將每個ThreesixtyViewer的圖像一起保存到與特定查看器關聯的一個目錄中。例如:

/public/system/threesixty_viewer_images/12/large/filename.jpg 

在這個例子中,在路徑中將是具體threesixtyviewer的ID - 但我無法找到該功能的任何實例。如果ThreesixtyViewer爲57的ID,那麼路徑看起來像這樣:

/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/ 

我知道:路徑和:url屬性需要更新編輯爲threesixty_viewer_image.rb內has_attached_file - 但我不確定如何可以得到threesixty_viewer的ID ...現在我在它的位置添加了一個:VIEWER_ID。

任何幫助將不勝感激!預先感謝任何可以借鑑的人!

回答

2

您可以將任何模型屬性添加到該對象的該路徑。我相信你甚至可以添加任何方法將會響應的東西,所以你甚至可以創建路徑助手來返回特定的字符串(比如它保存的月份,年份等)。

對你而言,ThreesixtyViewerImage是一個子模型,你的表應該包含一個父模型的列。在你的情況,該屬性可能是:threesixty_viewer_id

以下是我認爲你需要設置上threesixty_viewer_image.rb該路徑:

has_attached_file :image, 
    styles: { small: "500x500#", large: "1440x800>" }, 
    path: ":rails_root/public/system/:class/:threesixty_viwer_id/:size/:filename", 
    url: "/system/:class/:threesixty_viewer_id/:size/:filename" 

validates_attachment_content_type :image, content_type: /\Aimage\/.*\Z/ 

編輯

我的一切上面說的是錯誤的。 我的歉意!你需要使用的是Paperclip::InterpolationHere's a link

這裏就是我所做的利用:

  1. 創建一個新的文件:config/initializers/paperclip_interpolators
  2. 廣場這樣的事情在該文件中:

    Paperclip.interpolates :threesixty_viewer_id do |attachment, style| 
        attachment.instance.threesixty_viewer_id 
    end 
    
  3. 重新啓動應用程序
  4. 重新生成附件的路徑/文件夾。 Here's a Link

無論何時您想要路徑中的其他屬性,只需添加另一個插補器!對不起,以前誤導你。

+0

當我用**:threesixty_viewer_id **或任何其他應該可用的屬性(即:article_id)執行此操作時 - 它將目錄保存爲完全一樣的...所以它現在看起來像'/ public/system/threesixty_viewer_images /:threesixty_viewer_id/large/filename.jpg' –

+0

更新了我的回答,並提供了有關插值以及如何使用它們的更多具體細節。 –

1

@colin_hagan是正確的道路上 - 我建議你看看Paperclip Interpolations

#app/models/threesixty_viewer_image.rb 
class ThreesixtyViewerImage < ActiveRecord::Base 
    belongs_to :threesixty_viewer 

    has_attached_file :image, 
     path: ":rails_root/public/system/:class/:threesixty_viewer_id/:size/:filename", 
     url: "/system/:class/:threesixty_viewer_id/:size/:filename" 

    Paperclip.interpolates :threesixty_viewer_id do |attachment, style| 
     attachment.instance.threesixty_viewer_id 
    end 
end 

注意雙引號,而不是單一的。

雙引號應該用於插值 - 單引號是文字字符串(我認爲是)。


我發現了一些時間回到另一件事是paperclip_defaults選項 - 允許你指定styles等任何附件:

#config/application.rb 
... 
config.paperclip_defaults = { 
    styles: { small: "500x500#", large: "1440x800>" } 
} 

這些都可以在各自的車型覆蓋 - 它只是得心應手因爲這意味着您每次有附件時都不必明確定義styles

+0

這照顧它謝謝! paperclip_defaults也是一個很好的提示,一定會在我的下一個項目中使用它!再次感謝! –