2012-02-17 141 views
-1

我正在使用Paperclip with Rails来保存附件。附件保存后我需要做些什么。问题是after_photo_post_process(以及after_post_process)在附件实际调整大小和保存之前被调用。Rails回形针:在保存附件后执行某些操作

下面是代码:

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 

我不知道为什么会出现这种情况?

更新:使用Rails.logger给出了从服务器的输出:

{: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仍称实际上保存在安装前。

回答

1

你的输出不能说如果你正确与否。

在你的方法中,你用puts输出,但所有其他输出变成Rails.logger。并且两者都不在同一时间显示。

puts命令在调用时立即刷新。但Rails.logger缓冲区化所有日志并仅在请求完成时才刷新。

在你的情况下,after_photo_post_process是在请求结束之前完成的,所以它在之前显示。在你真的希望看到你的代码是真正的呼叫时,使用Rails.logger.info而不是puts

+0

感谢您的提示,将调试更多。 – 2012-02-17 12:45:40

+0

并更新。不过,在调用after_photo_post_process之后,附件仍会保存。 – 2012-02-17 14:55:05

+0

这是after_post_process的正常行为,处理后和保存之前请参阅文档。 – shingara 2012-02-17 15:22:39

相关问题