在对象初始化之前调用回形针动态 Proc 样式
我有以下回形针设置。发生的情况是我正在使用一个过程来设置各种样式的尺寸。但是,该过程会在 new 和 super 调用期间被调用。我浏览了调试器,似乎它首先处理 :photo 参数,因此它初始化附件并调用样式过程,此时实际对象(照片)尚未由传入的参数初始化(特别是 photo.gallery_id所以它没有正确设置样式,我什至尝试了重新处理,但没有帮助我花了几天时间,但仍然没有任何帮助!
class Photo < ActiveRecord::Base
has_and_belongs_to_many :staffs
has_attached_file :photo,
:storage => :s3,
:s3_credentials => "#{RAILS_ROOT}/config/s3.yml",
:path => "/assets/:id/:class/:style/:image_name.:extension",
:url => "/assets/:id/:class/:style/:image_name.:extension",
:styles => Proc.new { |clip| clip.instance.attachment_styles}
def attachment_styles
if self.gallery.nil?
{ :original => {
:processors => [:watermark],
:geometry =>"600x800!",
:watermark_path => ':rails_root/public/images/watermark.png',
:position => 'SouthEast'},
:thumbnail => {
:processors => [:watermark],
:geometry => "200x300!",
:watermark_path => ':rails_root/public/images/watermark.png',
:position => 'SouthEast'}
}
elsif self.photo.styles.empty?
gallery_type = GalleryType.find_by_id(self.gallery_id)
{ :original => {
:processors => [:watermark],
:geometry =>"#{gallery_type.width_max}x#{gallery_type.height_max}!",
:watermark_path => ':rails_root/public/images/watermark.png',
:position => 'SouthEast'},
:thumbnail => {
:processors => [:watermark],
:geometry => "#{gallery_type.width_min}x#{gallery_type.height_min}!",
:watermark_path => ':rails_root/public/images/watermark.png',
:position => 'SouthEast'}
}
else
self.photo.styles
end
end
def reprocess_att
self.photo.reprocess!
end
def initialize(galleryid, params = {})
begin
param.merge!({"gallery_id" => galleryid.to_s})
super(params)
rescue => e
puts e.message()
end
end
I have the following paperclip setup. What happens is that I'm using a proc to set the sizes for various styles. However, the proc gets called on new and during the super call. I walked through the debugger and it seems like it processes the :photo parameter first so it initializes the attachment and calls the styles proc at which point the actual object (Photo) has not been initialized by the passed in params(particularly the photo.gallery_id so it doesn't set the styles correctly. I even tried reprocessing and it didn't help. I've spent a couple of days on this and still no luck. any help is appreciated!
class Photo < ActiveRecord::Base
has_and_belongs_to_many :staffs
has_attached_file :photo,
:storage => :s3,
:s3_credentials => "#{RAILS_ROOT}/config/s3.yml",
:path => "/assets/:id/:class/:style/:image_name.:extension",
:url => "/assets/:id/:class/:style/:image_name.:extension",
:styles => Proc.new { |clip| clip.instance.attachment_styles}
def attachment_styles
if self.gallery.nil?
{ :original => {
:processors => [:watermark],
:geometry =>"600x800!",
:watermark_path => ':rails_root/public/images/watermark.png',
:position => 'SouthEast'},
:thumbnail => {
:processors => [:watermark],
:geometry => "200x300!",
:watermark_path => ':rails_root/public/images/watermark.png',
:position => 'SouthEast'}
}
elsif self.photo.styles.empty?
gallery_type = GalleryType.find_by_id(self.gallery_id)
{ :original => {
:processors => [:watermark],
:geometry =>"#{gallery_type.width_max}x#{gallery_type.height_max}!",
:watermark_path => ':rails_root/public/images/watermark.png',
:position => 'SouthEast'},
:thumbnail => {
:processors => [:watermark],
:geometry => "#{gallery_type.width_min}x#{gallery_type.height_min}!",
:watermark_path => ':rails_root/public/images/watermark.png',
:position => 'SouthEast'}
}
else
self.photo.styles
end
end
def reprocess_att
self.photo.reprocess!
end
def initialize(galleryid, params = {})
begin
param.merge!({"gallery_id" => galleryid.to_s})
super(params)
rescue => e
puts e.message()
end
end
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
据我所知,参数的顺序很重要。我:
这并没有正确设置样式。他们是零。我将其更改为:
然后代码:
完美运行。希望这有帮助。
From what I can see, the order of the parameters is important. I had:
And this wasn't setting up the styles correctly. They they were nil. I changed it to:
And then the code:
worked perfectly. Hope this helps.
感谢您的回答!
我已经为此奋斗了几个星期。我使用 Paperclip 和 FFMPEG 来制作上传视频的缩略图。我可以选择设置用作缩略图的帧。
我还使用嵌套表单(很棒的嵌套表单)来上传资源。 所以我所做的是将帧时间参数放在文件浏览按钮之前。这为我解决了问题,因为我没有使用构建器。
Thanks for the answer!
I've been fighting with this for a few weeks now. I'm using Paperclip with FFMPEG to make thumbnails of uploaded videos. I have an option to set what frame to use as the thumbnail.
I'm also using a nested form (awesome nested forms) for my asset upload. So what I did was put the frame time param before the file browse button. This solved the problem for me since I'm not using builder.