[英]rails paperclip customize upload url path
This is my upload.rb 这是我的upload.rb
class Upload < ActiveRecord::Base
belongs_to :post
has_attached_file :upload,styles: { medium: ["500x400>",:jpg], thumb: ["100x100>",:jpg]},url: "/post_images/post_:postid/:style/:filename"
def postid
self.post_id
end
end
I have a column with post_id. 我有一个post_id列。 As belongs_to
represents i will have more than one images for one post. 由于belongs_to
表示我将为一个帖子拥有多个图像。 But while saving files in folder instead of post_25
. 但是在文件夹而不是post_25
保存文件。 It is storing as post_:postid
它存储为post_:postid
But if i give as :id
it is working. 但如果我给出:id
它正在工作。
How can i solve it. 我该如何解决呢。 can anyone help with this. 有人能帮忙吗。
You should be using Paperclip's interpolations to achieve this functionality. 您应该使用Paperclip的插值来实现此功能。 First, start by defining an interpolation in an initializer: 首先,首先在初始化程序中定义插值:
# config/initializers/interpolations.rb
Paperclip.interpolates :postid do |attachment, style|
'post_' + attachment.instance.post.id
end
Then, you can use the :postid
interpolation directly in your attachment URL declaration (remember to restart your server first): 然后,您可以直接在附件URL声明中使用:postid
插值(请记住首先重新启动服务器):
# app/models/upload.rb
has_attached_file :upload,styles: { medium: ["500x400>",:jpg], thumb: ["100x100>",:jpg]},url: "/post_images/:postid/:style/:filename"
Note that :postid
is not merely the instance method you've defined in your model – Paperclip exclusively utilizes interpolations to define dynamic variables within URL/path declarations. 需要注意的是:postid
不仅是你已经在你的模型中定义的实例方法-回形针专门利用插值来定义URL /路径声明中的动态变量。
In one of my models I have 在我的一个模型中
Paperclip.interpolates :invoice_file_name do |attachment, style|
attachment.instance.invoice_file_name
end
which was taken from Paperclip wiki on Github. 这是取自Github上的Paperclip wiki。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.