我在使用回形针上传多个附件并使用水印处理它们时遇到了很多麻烦。
我有 2 个模型,广告和照片。广告 has_many :photos 和照片 belongs_to :ad。
在 /models/ad.rb 中更准确地说,我有:
class Ad < ActiveRecord::Base
has_many :photos, :dependent => :destroy
accepts_nested_attributes_for :photos, :allow_destroy => true
end
我的 photo.rb 文件如下所示:
class Photo < ActiveRecord::Base
belongs_to :ad
has_attached_file :data,
:styles => {
:thumb => "100x100#",
:first => {
:processors => [:watermark],
:geometry => '300x250#',
:watermark_path => ':rails_root/public/images/watermark.png',
:position => 'SouthEast' },
:large => {
:processors => [:watermark],
:geometry => '640x480#',
:watermark_path => ':rails_root/public/images/watermark.png',
:position => 'SouthEast' }
}
end
在我看来,我用它来添加文件字段
<% f.fields_for :photos do |p| %>
<%= p.label :data, 'Poza:' %> <%= p.file_field :data %>
<% end %>
在我的控制器中,在我4.times {@ad.photos.build}
用来生成文件字段的编辑操作中。
如果我不使用水印处理器,如果我使用普通的 has_attached_file 声明,这一切都可以正常工作,如下所示:
has_attached_file :data,
:styles => {
:thumb => "100x100#",
:first => '300x250#',
:large => '640x480#'
}
但是当我使用水印处理器时,我总是会收到这个错误:
NoMethodError in PublicController#update_ad
You have a nil object when you didn't expect it!
You might have expected an instance of ActiveRecord::Base.
The error occurred while evaluating nil.[]
..............................
/usr/lib/ruby/gems/1.8/gems/activerecord-2.3.5/lib/active_record/nested_attributes.rb:350:in `assign_nested_attributes_for_collection_association'
/usr/lib/ruby/gems/1.8/gems/activerecord-2.3.5/lib/active_record/nested_attributes.rb:345:in `each'
/usr/lib/ruby/gems/1.8/gems/activerecord-2.3.5/lib/active_record/nested_attributes.rb:345:in `assign_nested_attributes_for_collection_association'
/usr/lib/ruby/gems/1.8/gems/activerecord-2.3.5/lib/active_record/nested_attributes.rb:243:in `photos_attributes='
/usr/lib/ruby/gems/1.8/gems/activerecord-2.3.5/lib/active_record/base.rb:2746:in `send'
/usr/lib/ruby/gems/1.8/gems/activerecord-2.3.5/lib/active_record/base.rb:2746:in `attributes='
/usr/lib/ruby/gems/1.8/gems/activerecord-2.3.5/lib/active_record/base.rb:2742:in `each'
/usr/lib/ruby/gems/1.8/gems/activerecord-2.3.5/lib/active_record/base.rb:2742:in `attributes='
/usr/lib/ruby/gems/1.8/gems/activerecord-2.3.5/lib/active_record/base.rb:2628:in `update_attributes'
/home/alexg/Sites/vandmasina/app/controllers/public_controller.rb:217
/home/alexg/Sites/vandmasina/app/controllers/public_controller.rb:216:in `update_ad'
参数还可以,据我所知
Parameters:
{"commit"=>"Salveaza modificarile",
"ad"=>{"price"=>"6000",
"oras"=>"9",
"photos_attributes"=>{"0"=>{"data"=>#<File:/tmp/RackMultipart20100928-5130-b42noe-0>},
"1"=>{"data"=>#<File:/tmp/RackMultipart20100928-5130-r0ukcr-0>},
"2"=>{"data"=>#<File:/tmp/RackMultipart20100928-5130-mb23ei-0>},
"3"=>{"data"=>#<File:/tmp/RackMultipart20100928-5130-1bpkm3b-0>}},
水印处理器 /lib/paperclip_processors/watermark.rb 如下所示:
module Paperclip
class Watermark < Processor
class InstanceNotGiven < ArgumentError;
end
def initialize(file, options = {},attachment = nil)
super
@file = file
@current_format = File.extname(@file.path)
@basename = File.basename(@file.path, @current_format)
@watermark = ':rails_root/public/images/watermark.png'
@current_geometry = Geometry.from_file file # This is pretty slow
@watermark_geometry = watermark_dimensions
end
def watermark_dimensions
return @watermark_dimensions if @watermark_dimensions
@watermark_dimensions = Geometry.from_file @watermark
end
def make
dst = Tempfile.new([@basename, @format].compact.join("."))
watermark = " \\( #{@watermark} -extract #{@current_geometry.width.to_i}x#{@current_geometry.height.to_i}+#{@watermark_geometry.height.to_i /
2}+#{@watermark_geometry.width.to_i / 2} \\) "
command = "-gravity center " + watermark + File.expand_path(@file.path) + " " +File.expand_path(dst.path)
begin
success = Paperclip.run("composite", command.gsub(/\s+/, " "))
rescue PaperclipCommandLineError
raise PaperclipError, "There was an error processing the watermark for #{@basename}" if @whiny_thumbnails
end
dst
end
end
end
我已经在普通应用程序中尝试过处理器,没有多个附件,它运行完美。据我所知,它不适用于nested_attributes。
该应用程序是带有 ruby 1.8.7 和回形针 2.3.11 的 rails 2.3.5
如果您能提供任何帮助,我们将不胜感激,因为我已经尝试了 2 天了:)