2012-02-17 4 views
-1

첨부 파일 저장을 위해 Rails와 함께 Paperclip을 사용하고 있습니다. 첨부 파일을 저장 한 후에 뭔가해야합니다. 문제는 첨부 파일이 실제로 크기가 조정되고 저장되기 전에 after_photo_post_process (after_post_process는 물론)가 호출된다는 것입니다. 여기 레일즈 Paperclip : 첨부 파일을 저장 한 후에 작업하기

코드입니다 :

after_photo_post_process :update_facebook 
#after_create :update_facebook 

    def update_facebook 
    puts "UPDATE_FACEBOOK" 
    puts item_id.to_s 
     if([email protected]) 
      @graph = Koala::Facebook::GraphAPI.new(User.find(Item.find(item_id).user_id).fbprofile.access_token) 
     end 

     options = { 
      :message => "I've just added " + Item.find(item_id).title + " to my 111 items list. Check it out: http://111items.com/items/" + item_id.to_s, 
      :picture => "http://localhost:3000" + photo.url(:small), 
      :link => "http://111items.com/items/" + item_id.to_s 
      } 

     puts options 

     @graph.put_object("me", "feed", options) 
    end 

그리고 여기에 서버 출력 :

UPDATE_FACEBOOK 
157 
{:message=>"I've just added calculator to my 111 items list. Check it out: http://111items.com/items/157", :picture=>"http://localhost:3000/system/photos/21/small/casio_fx-82sx.jpg?1329477882", :link=>"http://111items.com/items/157"} 


Started POST "/items" for 127.0.0.1 at 2012-02-17 13:24:42 +0200 
    Processing by ItemsController#create as HTML 
    Parameters: {"utf8"=>"✓", "authenticity_token"=>"xbDLXeQlld/fDa7bZQZPhzr+OHUAYvFgH1h/CTNCLIQ=", "item"=>{"title"=>"calculator", "description"=>"", "public"=>"1", "secure_details"=>"", "bookmark_id"=>"152", "warranty_until"=>"", "assets_attributes"=>{"0"=>{"photo"=>#<ActionDispatch::Http::UploadedFile:0x00000004b89d78 @original_filename="casio_fx-82sx.jpg", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"item[assets_attributes][0][photo]\"; filename=\"casio_fx-82sx.jpg\"\r\nContent-Type: image/jpeg\r\n", @tempfile=#<File:/tmp/RackMultipart20120217-8212-1a67ce6>>}}}, "commit"=>"Create Item"} 
    User Load (0.1ms) SELECT `users`.* FROM `users` WHERE `users`.`id` = 30 LIMIT 1 
[paperclip] identify -format %wx%h '/tmp/stream20120217-8212-i7zn7g.jpg[0]' 2>/dev/null 
[paperclip] convert '/tmp/stream20120217-8212-i7zn7g.jpg[0]' -resize "80x" -crop "80x80+0+13" +repage '/tmp/stream20120217-8212-i7zn7g20120217-8212-m4rkbm' 2>/dev/null 
[paperclip] identify -format %wx%h '/tmp/stream20120217-8212-i7zn7g.jpg[0]' 2>/dev/null 
[paperclip] convert '/tmp/stream20120217-8212-i7zn7g.jpg[0]' -resize "150x150>" '/tmp/stream20120217-8212-i7zn7g20120217-8212-14hjb17' 2>/dev/null 
[paperclip] Saving attachments. 
[paperclip] saving /home/alex/hundredthings/public/system/photos/21/original/casio_fx-82sx.jpg 
[paperclip] saving /home/alex/hundredthings/public/system/photos/21/thumb/casio_fx-82sx.jpg 
[paperclip] saving /home/alex/hundredthings/public/system/photos/21/small/casio_fx-82sx.jpg 
    SQL (76.6ms) COMMIT 
Redirected to http://localhost:3000/pages/home 
    CACHE (0.0ms) SELECT `fbprofiles`.* FROM `fbprofiles` WHERE (`fbprofiles`.user_id = 30) LIMIT 1 
Completed 302 Found in 2961ms 

내가 왜 이런 일이 있을까?

업데이트 : 첨부 파일이 실제로 저장하기 전에

{:message=>"I've just added Another one to my 111 items list. Check it out: http://111items.com/items/165", :picture=>"http://111items.com/images/rails.png", :link=>"http://111items.com/items/165"} 
[paperclip] Saving attachments. 
[paperclip] saving /home/alex/hundredthings/public/system/photos/29/original/crontab.png 
[paperclip] saving /home/alex/hundredthings/public/system/photos/29/thumb/crontab.png 
[paperclip] saving /home/alex/hundredthings/public/system/photos/29/small/crontab.png 

그래서, after_photo_post_process 여전히 호출됩니다 Rails.logger를 사용하여 서버에서이 출력을 제공합니다.

답변

1

출력 결과가 옳은지 말할 수 없습니다.

당신의 방법에서는 puts으로 출력하지만 다른 모든 출력은 Rails.logger이됩니다. 둘 다 같은 시간에 표시되지 않습니다.

puts 명령은 즉시 호출됩니다. Rails.logger은 모든 로그를 버퍼링하고 요청이 완료 될 때만 플러시합니다.

after_photo_post_process은 요청이 끝나기 전에 완료되므로 이전에 표시됩니다. 실제로 코드가 실제로 호출되는 것을보고 싶다면 puts 대신 Rails.logger.info을 사용하십시오.

+0

힌트를 보내 주셔서 감사합니다. –

+0

업데이트되었습니다. 그래도 첨부 파일은 after_photo_post_process가 호출 된 후에 저장됩니다. –

+0

그것은 after_post_process의 정상적인 동작입니다. 처리 후 및 저장하기 전에 설명서를 참조하십시오. – shingara

관련 문제