저는 이미지를 저장하는 새 프로젝트에서 Paperclip gem을 사용하고 있습니다. 또한 내 모델의 가상 속성에 저장된 자르기 매개 변수에 따라 이미지를 자르기 위해 작성한 간단한 사용자 정의 Paperclip 프로세서가 있습니다.가상 속성에 값이 오기 전에 Paperclip의 후 처리가 시작됩니까?
그래서 클라이언트는 모델의 실제 db 필드와 crop_x, crop_y, crop_w, crop_h 속성을 사용하여 양식을 전송합니다. 컨트롤러는 "params"를 기반으로 새 인스턴스를 만듭니다. 잘 살펴 보았지만 확인했는데 이상한 것들이 있습니다. 일어나고.
디버거 내 사용자 지정 프로세서에 배치
class Jewel < ActiveRecord::Base
has_many :line_items
belongs_to :material
******* some code *********
before_save :debugmeBS
validates :category_id, :presence => true
attr_accessor :crop_x, :crop_y, :crop_w, :crop_h
attr_accessible :name, :category_id, :price, :crop_x, :crop_y,
:crop_w, :crop_h, :image
after_update :reprocess_image, :if => :need_to_crop?
has_attached_file :image, :styles => { :full => "900x", :smallthumb => "80x80>" },
:processors => [:cropper, :thumbnail]
before_post_process :debugmePP
def need_to_crop?
# debugger
!crop_x.blank? && !crop_y.blank? &&!crop_w.blank? &&!crop_h.blank?
end
private
def debugmeBS
debugger
x=2
end
def debugmePP
debugger
x=3
end
end
이 crop_x, crop_y 등 가상 속성이 비어있는 것을 보여줍니다 (호출하는 방법 "need_to_crop을?") 한편 다른 가상없는 사람이 제대로 설정 : 여기
내 모델의 . 추적 오류에 나는 before_save와 Paperclip의 before_post_process라는 두 개의 "before _"이벤트를 배치했습니다. before_post_process가 before_save 전에 호출되고, 모든 "crop_"속성은 nil이지만, before_save가 호출되는 순간에는 올바르게 설정됩니다.
그래서 질문은 왜입니까? Paperclip 프로세서가 액세스 할 가상 속성을 설정하는 방법은 무엇입니까? 고마워요
해킹,하지만 좋은 해킹 :) –