【发布时间】:2023-03-17 02:38:01
【问题描述】:
在我的控制器中,我有一个 after_action 块。当相关方法失败时,我想停止 after_action 运行。
Rails 4.2,红宝石 2.2
有谁知道如何在 Rails 中做到这一点?
class MyController < ApplicationController
after_action only: :show do
# do so and so
end
def show
begin
# something really bad happens here... My Nose!
rescue => e
# how do I stop after_action from running?
flash[:error] = 'Oh noes... Someone make a log report'
redirect_to '/'
return
end
# yarda yarda yarda
end
end
【问题讨论】:
-
为什么不创建一个函数并在 show(action) 方法中使用条件调用它。
-
我删除了它,但我在 :show 上使用了动作缓存。
-
浏览文档,你不可能有条件回调一个动作。我想你必须在你的回调块中加入一些逻辑。否则等待更好的意见。
标签: ruby-on-rails controller ruby-on-rails-4.2