ROR在一个语句中附加多个文件

时间:2015-08-30 01:03:11

标签: ruby-on-rails paperclip

有没有办法缩短这段代码?它看起来很悲惨

has_attached_file :logo, :styles => { :medium => "300x300>", :thumb => "100x100>" }, :default_url => "/images/:style/missing.png"
has_attached_file :image1, :styles => { :medium => "300x300>", :thumb => "100x100>" }, :default_url => "/images/:style/missing.png"
has_attached_file :image2, :styles => { :medium => "300x300>", :thumb => "100x100>" }, :default_url => "/images/:style/missing.png"
has_attached_file :image3, :styles => { :medium => "300x300>", :thumb => "100x100>" }, :default_url => "/images/:style/missing.png"

1 个答案:

答案 0 :(得分:1)

你可以这样做:

[:logo, :image1, :image2, :image3].each do |image_symbol| 
   has_attached_file image_symbol, :styles => { :medium => "300x300>", :thumb => "100x100>" }, 
          :default_url => "/images/:style/missing.png"  
end

希望这会有所帮助:)。