Ruby on rails Rails回形针:保存附件后执行某些操作

Ruby on rails Rails回形针:保存附件后执行某些操作,ruby-on-rails,ruby,paperclip,Ruby On Rails,Ruby,Paperclip,我使用带导轨的回形针来保存附件。保存附件后,我需要执行一些操作。问题是在实际调整和保存附件之前,调用了after_photo_post_进程(以及after_post_进程) 代码如下: after_photo_post_process :update_facebook #after_create :update_facebook def update_facebook puts "UPDATE_FACEBOOK" puts item_id.to_s i

我使用带导轨的回形针来保存附件。保存附件后,我需要执行一些操作。问题是在实际调整和保存附件之前,调用了after_photo_post_进程(以及after_post_进程)

代码如下:

after_photo_post_process :update_facebook
#after_create :update_facebook

    def update_facebook
    puts "UPDATE_FACEBOOK"
    puts item_id.to_s
        if(!@graph)
            @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

因此,在实际保存附件之前,仍然会调用\u photo\u post\u过程。

您的输出无法判断您是否正确

在您的方法中,您使用
put
输出,但所有其他输出都来自
Rails.logger
。并且两者不同时显示

调用时,
命令立即刷新。但是
Rails.logger
只在请求完成时缓冲所有日志并刷新


在您的情况下,
在请求结束之前完成后处理
,因此它在请求结束之前显示。如果你真的想知道你的代码何时真正被调用,请使用
Rails.logger.info
而不是
put

谢谢你的提示,将调试更多。并更新。尽管如此,在调用after_photo_post_进程后仍会保存附件。这是after_post_进程的正常行为,在处理之后和保存之前请参阅文档。好的,看起来是这样的。但是,在保存所有附件时,是否有任何方法可以捕获事件?我需要在facebook wall实际存在后用图片更新它。after_create不起作用,但我会再次尝试after_save。
{: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