使用回形针,如何将图像位置更改为“:parent_model_id /:id”文件夹格式?
鉴于我有一个包含许多图像的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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
啊,我终于想通了。我需要使用Paperclip.interpolates。
thoughtbot 的这篇文章 有点解释了,但有点过时了。
首先,创建一个 config/initializers/paperclip.rb 文件并添加以下内容:
这意味着现在在我的图像模型中我可以像这样引用 :listing_id:
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:
Which means that now in my images model I can refer to :listing_id like so:
PS: You need to restart the server before the changes in initializers.rb take effect.
您需要传递 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.
由于您的
Image
模型上已存在belongs_to
关系,因此您应该能够使用listing_id
作为回形针配置的一部分:Since you've got a
belongs_to
relationship on yourImage
model, you should just be able to uselisting_id
as part of the paperclip config: