【问题标题】:Redirect after a particular action特定操作后重定向
【发布时间】:2014-08-27 13:07:18
【问题描述】:

我一直面临着邮箱 gem 的一些问题。特别是,邮件被回复,并且选择删除时的邮件也被发送给垃圾,但我还没有在它之后成功将其重定向到收件箱。我总是不断收到错误“找不到具有 id 的用户”以下是垃圾部分的控制器代码 -

  def trash

      conversation = Conversation.find(params[:id])

      beta = current_userA || current_userB

         case beta

          when current_userA

            conversation.move_to_trash(current_userA)

            # redirect_to :conversations

          when current_userB

           conversation.move_to_trash(current_userB)

           # redirect_to :conversations

         end



    respond_to do |format|      
       format.html { redirect_to conversation_path, notice: 'Conversation successfully trashed.' }        
    end

end

日志

Started GET "/conversations/36" for 127.0.0.1 at 2014-08-28 10:19:56 +0530
Processing by ConversationsController#show as HTML

  Parameters: {"id"=>"36"}

  Message Load (0.5ms)  SELECT "notifications".* FROM "notifications" WHERE "notifications"."type" IN ('Message') AND "notifications"."id" = $1 LIMIT 1  [["id", "36"]]

  Conversation Load (0.4ms)  SELECT "conversations".* FROM "conversations" WHERE "conversations"."id" = 34 LIMIT 1

Completed 404 Not Found in 4ms

ActiveRecord::RecordNotFound (Couldn't find userA without an ID):

  app/controllers/conversations_controller.rb:160:in `show'


  Rendered /usr/local/rvm/gems/ruby-1.9.3-p545/gems/actionpack-3.2.13/lib/action_dispatch/middleware/templates/rescues/_trace.erb (1.2ms)

  Rendered /usr/local/rvm/gems/ruby-1.9.3-p545/gems/actionpack-3.2.13/lib/action_dispatch/middleware/templates/rescues/_request_and_response.erb (1.0ms)

  Rendered /usr/local/rvm/gems/ruby-1.9.3-p545/gems/actionpack-3.2.13/lib/action_dispatch/middleware/templates/rescues/diagnostics.erb within rescues/layout (19.7ms)

更新日志

将redirect_to 对话更改为redirect_to 对话后

Started POST "/conversations/38/trash" for 127.0.0.1 at 2014-08-28 12:29:16 +0530
Processing by ConversationsController#trash as HTML
  Parameters: {"authenticity_token"=>"CuXzMKNmD87qvfOuer4uhyTdysFS/5NxtfMZU3/Y5j0=", "id"=>"38"}
  Conversation Load (0.4ms)  SELECT "conversations".* FROM "conversations" WHERE "conversations"."id" = $1 LIMIT 1  [["id", "38"]]
  Filmmaker Load (0.7ms)  SELECT "userA".* FROM "userA" WHERE "userA"."id" = 67 LIMIT 1
  Receipt Load (0.8ms)  SELECT "receipts".* FROM "receipts" INNER JOIN "notifications" ON "notifications"."id" = "receipts"."notification_id" AND "notifications"."type" IN ('Message') WHERE "notifications"."conversation_id" = 38 AND "receipts"."receiver_id" = 67 AND "receipts"."receiver_type" = 'userA'
  SQL (11.3ms)  UPDATE "receipts" SET "trashed" = 't' WHERE (id = 82 )
  User Load (29.7ms)  SELECT "users".* FROM "users" 
Completed 500 Internal Server Error in 110ms

NameError (undefined local variable or method `conversations' for #<ConversationsController:0x10961634>):
  app/controllers/conversations_controller.rb:101:in `block (2 levels) in trash'
  app/controllers/conversations_controller.rb:100:in `trash'

请指教我做错了什么,我已经做了一天了,我还没有找到任何解决方案。不用说,userA 和 userB 都是不同的设计用户。欢迎提出任何建议。

【问题讨论】:

  • 为什么重定向被注释掉了?确切的异常消息是什么(请复制并粘贴)?在哪一行出现错误,我在这里没有看到任何User.find...?
  • @Michal 我明天肯定会发布错误,因为目前我无法访问我的机器。在此之前,您能否将您的答案发布在我应该在哪里编写 user.find 代码
  • 不看代码我无法给你答案。而且上面的代码显然是不完整的。
  • @MichalSzyndel 这是垃圾的控制器代码,请告诉我你还需要什么。
  • @MichalSzyndel - 发布操作日志

标签: ruby-on-rails ruby-on-rails-3 devise ruby-on-rails-3.2 mailboxer


【解决方案1】:

尝试在重定向中使用path,您可以从rake_routes 获得。也许下面给出的例子对你有用。

respond_to do |format|
    format.html { redirect_to inbox_path, notice: 'Conversation successfully trashed.' }    
end

【讨论】:

    猜你喜欢
    • 2021-11-29
    • 1970-01-01
    • 1970-01-01
    • 2011-04-21
    • 2012-03-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-22
    相关资源
    最近更新 更多