有条件地将样式应用于Rails 3.1中的回形针附件 [英] Conditionally applying styles to paperclip attachments in Rails 3.1
问题描述
我有以下型号:
Post.rb
class Post< ActiveRecord :: Base
belongs_to:category
has_many:attachments,:dependent => :销毁
has_many:引用,:依赖=> :销毁
validates:title,:category_id,:content,:presence => true
acts_as_taggable_on:keywords
accept_nested_attributes_for:attachments,:allow_destroy => true,
:reject_if => proc {|属性|属性['photo']。blank?}
accept_nested_attributes_for:citations,:allow_destroy => true
end
Attachment.rb
class附件< ActiveRecord :: Base
belongs_to:post
has_attached_file:photo,:styles => {:medium => 637x471>,
:thumb => Proc.new {| instance | instance.resize},
:carousel => Proc.new {| instance | instance.decide_style}
},
:url => /pictures/:style/:basename.:extension,
:path =>:rails_root / public / pictures /:style /:basename。:extension
validates_attachment_content_type:photo,:content_type => ['image / png','image / jpg','image / jpeg']
validates_attachment_size:photo,:less_than => 2.megabytes
### End Paperclip ####
def decide_style
#catid = Post.find(param [:post_category_id] )
#在这里可以从表单中选择。
if(catid == 2)
#400x800>或自定义样式
结束
$ b $结束
def resize
geo = Paperclip :: Geometry.from_file(photo.to_file(:原始))
ratio = geo.width / geo.height
min_width = 142
min_height = 119
如果比例> 1
#水平图像
final_height = min_height
final_width = final_height *比例
#{final_width.round} x#{final_height.round}!
else
#垂直图片
final_width = min_width
final_height = final_width *比例
#{final_height.round} x#{final_width.round}!
end
end
end
我是试图有条件地应用样式,基于从表单上的下拉菜单中选择。
想法?
p> Rishi
看起来你正在尝试将它基于 category_id
值在 Post
模型上,对吗?如果是这样,您实际上可以在附件选项中传递一个lambda作为:styles 条目的值,其中包括附件作为属性。
has_attached_file:photo,
:styles => lambda {| attachment | {
:medium => 637x471>,
:thumb => attachment.instance.resize,
:carousel => attachment.instance.decide_style,
}},
:url => /pictures/:style/:basename.:extension,
:path =>:rails_root / public / pictures /:style /:basename。:extension
Paperclip :: Attachment
对象,然后调用实例
方法返回你的模型实例。
decide_style
方法,您可以根据需要将它基于模型的 category_id
值。 def decision_style
case category_id
when 1 then200x400>
当2然后400x800>
else50x50#
end
end
I have the following models:
Post.rb
class Post < ActiveRecord::Base
belongs_to :category
has_many :attachments, :dependent => :destroy
has_many :citations, :dependent => :destroy
validates :title, :category_id, :content, :presence =>true
acts_as_taggable_on :keywords
accepts_nested_attributes_for :attachments, :allow_destroy => true,
:reject_if => proc { |attributes| attributes['photo'].blank?}
accepts_nested_attributes_for :citations, :allow_destroy=>true
end
Attachment.rb
class Attachment < ActiveRecord::Base
belongs_to :post
has_attached_file :photo, :styles => { :medium => "637x471>",
:thumb => Proc.new { |instance| instance.resize },
:carousel => Proc.new { |instance| instance.decide_style }
},
:url => "/pictures/:style/:basename.:extension",
:path =>":rails_root/public/pictures/:style/:basename.:extension"
validates_attachment_content_type :photo, :content_type => ['image/png', 'image/jpg', 'image/jpeg']
validates_attachment_size :photo, :less_than => 2.megabytes
### End Paperclip ####
def decide_style
# catid = Post.find(param[:post_category_id])
# something here to get the selection from the form.
if(catid == 2)
# "400x800>" or custom style here
end
end
def resize
geo = Paperclip::Geometry.from_file(photo.to_file(:original))
ratio = geo.width/geo.height
min_width = 142
min_height = 119
if ratio > 1
# Horizontal Image
final_height = min_height
final_width = final_height * ratio
"#{final_width.round}x#{final_height.round}!"
else
# Vertical Image
final_width = min_width
final_height = final_width * ratio
"#{final_height.round}x#{final_width.round}!"
end
end
end
I am trying to conditionally apply a style, based on the selection made from a drop down on the form. I am just not sure as to where the decision as to what style should be chosen is implemented.
Thoughts?
Rishi
It looks like you're trying to base it upon the category_id
value on the Post
model, is that right? If so, you can actually pass a lambda as the value of the :styles
entry in your attachment options, which includes the attachment as an attribute.
has_attached_file :photo,
:styles => lambda { |attachment| {
:medium => "637x471>",
:thumb => attachment.instance.resize,
:carousel => attachment.instance.decide_style,
} },
:url => "/pictures/:style/:basename.:extension",
:path =>":rails_root/public/pictures/:style/:basename.:extension"
The attachment
is a Paperclip::Attachment
object, and calling the instance
method on it returns your model instance.
Then in your decide_style
method, you can base it upon your model's category_id
value as needed.
def decide_style
case category_id
when 1 then "200x400>"
when 2 then "400x800>"
else "50x50#"
end
end
这篇关于有条件地将样式应用于Rails 3.1中的回形针附件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!