2012-01-31 28 views
7

tôi có các mô hình sau:phong cách có điều kiện áp dụng để kẹp giấy đính kèm trong Rails 3.1

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 

Tôi cố gắng để có điều kiện áp dụng một kiểu , dựa trên lựa chọn được thực hiện từ menu thả xuống trên biểu mẫu. Tôi chỉ không chắc chắn về việc quyết định chọn phong cách nào được thực hiện.

Suy nghĩ?

Rishi

Trả lời

15

Dường như bạn đang cố gắng để căn nó vào giá trị category_id trên mô hình Post, không biết có đúng? Nếu vậy, bạn thực sự có thể truyền một lambda làm giá trị của mục nhập :styles trong các tùy chọn đính kèm của bạn, bao gồm phần đính kèm dưới dạng thuộc tính.

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" 

Các attachment là một đối tượng Paperclip::Attachment, và gọi phương thức instance trên nó trả dụ mô hình của bạn.

Sau đó, trong phương thức decide_style, bạn có thể căn cứ vào giá trị category_id của mô hình nếu cần.

def decide_style 
    case category_id 
    when 1 then "200x400>" 
    when 2 then "400x800>" 
    else "50x50#" 
    end 
end 
+0

Cảm ơn Matt! Tôi đã giải quyết được vấn đề, nhưng cách tiếp cận của bạn là tốt. – frishi

Các vấn đề liên quan