【问题标题】:Rails Paperclip Gem - save parent model ID to pathRails Paperclip Gem - 将父模型 ID 保存到路径
【发布时间】: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


    【解决方案1】:

    您可以将任何模型属性添加到该对象的该路径。我相信您甚至可以添加该方法将响应的任何内容,因此您甚至可以创建用于返回特定字符串的路径助手(例如保存的月份、年份等)。

    在您的情况下,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

    任何时候你想在你的路径中添加另一个属性,只需添加另一个插值器!再次抱歉之前误导了您。

    【讨论】:

    • 当我使用 :threesixty_viewer_id 或任何其他应该可用的属性(即:article_id)执行此操作时 - 它会完全保存目录......所以它现在看起来喜欢/public/system/threesixty_viewer_images/:threesixty_viewer_id/large/filename.jpg
    • 用一些关于插值以及如何使用它们的更具体的细节更新了我的答案。
    【解决方案2】:

    @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

    【讨论】:

    • 这很好,谢谢! paperclip_defaults 也是一个很好的提示,肯定会计划在我的下一个项目中使用它!再次感谢!
    猜你喜欢
    • 2014-01-18
    • 1970-01-01
    • 2012-10-25
    • 1970-01-01
    • 2023-03-09
    • 2015-04-05
    • 2012-02-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多