2015-01-23 2 views
0

정의 클립 부착 프로세서 PARAM 파싱 순서 : 사용자가 지정한 처리하기 위해레일 (4) - I 일부 사용자 선택 자르기 설정과 함께 클립 이미지 첨부 콘텐츠 테이블을

create_table "content", force: true do |t| 
    t.string "title" 
    t.string "image_file_name" 
    t.string "image_content_type" 
    t.integer "image_file_size" 
    t.datetime "image_updated_at" 
    t.integer "crop_x" 
    t.integer "crop_y" 
    t.integer "crop_w" 
    t.integer "crop_h" 
end 

서버 측에 자르기 이미지, 난 here로부터 공급 정의 클립 프로세서를 가지고

내 문제가 crop_x, Y, H, w PARAMS는 화상 ATTAC 후 를 파싱되어
module Paperclip 
    class CustomCropper < Thumbnail 
    def initialize(file, options = {}, attachment = nil) 
     super 
     @current_geometry.width = target.crop_w 
     @current_geometry.height = target.crop_h 
    end 
    def target 
     @attachment.instance 
    end 
    def transformation_command 
     crop_command = [ 
      '-crop', 
      "#{target.crop_w}x" \ 
      "#{target.crop_h}+" \ 
      "#{target.crop_x}+" \ 
      "#{target.crop_y}", 
      '+repage' 
     ] 
     crop_command + super 
    end 
    end 
end 

따라서 사용자 지정 클립 프로세서는 모든 필드에 대해 nil을보고 이미지를 제대로 자르지 않습니다.

# rails processes the image_xxx params before the crop_xxx params 
@content = Content.new(content_params) 

첨부 이미지 전에 자르기 경계 필드를 처리하기 위해 레일을 말할 수있는 깨끗한 방법이 있나요? 또는 본질적으로이를 수행하는 다른 솔루션이 있습니까?

답변

2

내 인생에서 "깨끗한"방법을 찾을 수 없었습니다. 내가 마지막으로 수행 한 작업은 첨부 파일로 업데이트/저장을 호출하기 전에 crop_ 값을 저장하는 것이 었습니다. 예를 들어

는 :

def update 

    if article_params[:image_crop_y].present? 
    @article.image_crop_y = article_params[:image_crop_y] 
    @article.image_crop_x = article_params[:image_crop_x] 
    @article.image_crop_w = article_params[:image_crop_w] 
    @article.image_crop_h = article_params[:image_crop_h] 
    @article.save 
    end 

    if @article.update(article_params) 
    ... 
    end 

end 

나는 아니라 "깨끗한"방법, 말했듯이,하지만 나를 위해 효과적이었다.

+0

쿨, 고마워 Tim! DB 스키마의 이미지 첨부 필드 앞에 자르기 필드를 넣는 것이 훨씬 더 해킹 된 것 같습니다. (Rails는 스키마 순서로 params를 처리한다). 따라서 코드는 깨끗해 보이지만 Rails 구현에서 무언가가 변경되면 궁극적으로 위험합니다. –

+0

나는 똑같은 문제가 .. 솔루션 주셔서 감사! –