Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/56.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails Rails曲别针Gem-将父模型ID保存到路径_Ruby On Rails_Ruby_Ruby On Rails 4_Paperclip - Fatal编程技术网

Ruby on rails Rails曲别针Gem-将父模型ID保存到路径

Ruby on rails Rails曲别针Gem-将父模型ID保存到路径,ruby-on-rails,ruby,ruby-on-rails-4,paperclip,Ruby On Rails,Ruby,Ruby On Rails 4,Paperclip,我有一个ThreesixtyViewer模型,它还有一个嵌套资源ThreesixtyViewerImage模型。正在通过保存图像属性,但我在更新文件路径时遇到了问题 每个ThreesixtyViewer的图像需要一起保存在与特定查看器关联的一个目录中。例如: /public/system/threesixty_viewer_images/12/large/filename.jpg 在本例中,路径中的12将是特定threesixtyviewer的id-但我找不到任何具有该功能的示例。如果Thre

我有一个ThreesixtyViewer模型,它还有一个嵌套资源ThreesixtyViewerImage模型。正在通过保存图像属性,但我在更新文件路径时遇到了问题

每个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
360_viewer.rb

belongs_to :article

has_many :threesixty_viewer_images, dependent: :delete_all
accepts_nested_attributes_for :threesixty_viewer_images, allow_destroy: true
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/
360_查看器_image.rb

belongs_to :article

has_many :threesixty_viewer_images, dependent: :delete_all
accepts_nested_attributes_for :threesixty_viewer_images, allow_destroy: true
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/
我知道需要为ThreeSixture\u viewer\u image.rb中的has\u attached\u文件更新:path和:url属性-但我不确定如何获取ThreeSixture\u查看器的id。。。现在我在它的位置添加了一个:VIEWER\u ID


任何帮助都将不胜感激!提前感谢任何能看一眼的人

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

在您的情况下,ThreesixtyViewerImage是一个子模型,您的表应该包含父模型的一列。在您的情况下,该属性可能是:360\u viewer\u id

以下是我认为您需要在360_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/
编辑

我上面说的一切都是大错特错的。我道歉!您需要使用的是
回形针::插值

以下是我所做的工作:

  • 创建一个新文件:
    config/initializers/paperclip\u interpolators
  • 在该文件中放置类似的内容:

    Paperclip.interpolates :threesixty_viewer_id do |attachment, style|
      attachment.instance.threesixty_viewer_id
    end
    
  • 重新启动应用程序
  • 重新生成附件的路径/文件夹
    任何时候,只要在路径中添加另一个属性,就可以添加另一个插值器!再次为之前的误导感到抱歉。

    @colin_hagan
    走在正确的道路上-我建议您研究一下:


    这些可以在您各自的模型中覆盖-这对我来说很方便,因为这意味着您不必每次有附件时都显式定义
    样式。

    当我使用:360\u viewer\u id或任何其他应该可用的属性(即:article\u id)执行此操作时,它会将目录准确地保存为。。。因此,现在它看起来像是
    /public/system/360_viewer_images/:360_viewer_id/large/filename.jpg
    更新了我的答案,提供了一些关于插值和如何使用插值的更具体的细节。这很好,谢谢!回形针也是一个很好的技巧,我一定会计划在我的下一个项目中使用它!再次感谢!