使用回形针,如何将图像位置更改为“:parent_model_id /:id”文件夹格式?

发布于 2024-10-09 14:19:07 字数 573 浏览 2 评论 0原文

鉴于我有一个包含许多图像Listing模型,并且每个图像都有一个附件,我怎样才能拥有Listing_id 是文件夹结构的一部分吗?

像这样: system/photos/[listing_id]/:id

我知道使用 :id 会输出图像记录的 id。

这是我目前拥有的:

class Image < ActiveRecord::Base
belongs_to :listing #Rails ActiveRecord Relation. An image belongs to a post. 

# paperclip data
has_attached_file :photo, :styles => { :medium => "300x300>", :thumb => "100x100>" }, :url => "/public/system/:class/:attachment/:id/:style_:filename"

结束

Given that I have a Listing model that has many images and each image has one attachment, how can I have the listing_id be part of the folder structure?

Like so: system/photos/[listing_id]/:id

I know that using :id will output the id of the image record.

Here's what I currently have:

class Image < ActiveRecord::Base
belongs_to :listing #Rails ActiveRecord Relation. An image belongs to a post. 

# paperclip data
has_attached_file :photo, :styles => { :medium => "300x300>", :thumb => "100x100>" }, :url => "/public/system/:class/:attachment/:id/:style_:filename"

end

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

客…行舟 2024-10-16 14:19:07

啊,我终于想通了。我需要使用Paperclip.interpolates

thoughtbot 的这篇文章 有点解释了,但有点过时了。

首先,创建一个 config/initializers/paperclip.rb 文件并添加以下内容:

Paperclip.interpolates :listing_id do |attachment, style|
  attachment.instance.listing_id # or whatever you've named your User's login/username/etc. attribute
end

这意味着现在在我的图像模型中我可以像这样引用 :listing_id

class Image < ActiveRecord::Base
    belongs_to :listing #Rails ActiveRecord Relation. An image belongs to a post. 

    # paperclip data
    has_attached_file :photo, :styles => { :medium => "300x300>", :thumb => "100x100>" }, 
    :url => "/system/:attachment/:listing_id/:id/:style_:filename" #location where to output the server. :LISTING_ID is defined in config/initializers/paperclib.rb

end

PS:您需要重新启动服务器才能使initializers.rb 中的更改生效。

Ah, I finally figured it out. I needed to use Paperclip.interpolates.

This post from thoughtbot sort of explains it, but it's slightly outdated.

First, create a config/initializers/paperclip.rb file and add the following:

Paperclip.interpolates :listing_id do |attachment, style|
  attachment.instance.listing_id # or whatever you've named your User's login/username/etc. attribute
end

Which means that now in my images model I can refer to :listing_id like so:

class Image < ActiveRecord::Base
    belongs_to :listing #Rails ActiveRecord Relation. An image belongs to a post. 

    # paperclip data
    has_attached_file :photo, :styles => { :medium => "300x300>", :thumb => "100x100>" }, 
    :url => "/system/:attachment/:listing_id/:id/:style_:filename" #location where to output the server. :LISTING_ID is defined in config/initializers/paperclib.rb

end

PS: You need to restart the server before the changes in initializers.rb take effect.

锦上情书 2024-10-16 14:19:07

您需要传递 url 和 path 属性。请参阅此 Thoughtbot 博客 帖子 寻求帮助。你的方式很接近,但你需要传递我假设的listing_id,而不是:id。

you need to pass a url and path attribute. look at this thoughtbot blog post for help. The way you have it is close, but you need to pass the listing_id i would assume, not the :id.

雨落星ぅ辰 2024-10-16 14:19:07

由于您的 Image 模型上已存在 belongs_to 关系,因此您应该能够使用 listing_id 作为回形针配置的一部分:

has_attached_file :photo, 
    :styles => { :medium => "300x300>", :thumb => "100x100>" }, 
    :url => "system/photos/:listing_id/:id"

Since you've got a belongs_to relationship on your Image model, you should just be able to use listing_id as part of the paperclip config:

has_attached_file :photo, 
    :styles => { :medium => "300x300>", :thumb => "100x100>" }, 
    :url => "system/photos/:listing_id/:id"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文