我一直在Google上花费大量时间,试图弄清楚如何简单地使rails来显示我的错误,就像我处于生产模式一样(简单地,这样我就可以验证它是否符合我的期望)...
所以首先我读到您可以将config / environments / development.rb设置为
config.consider_all_requests_local = false
然后在应用程序 Controller 中执行以下操作:
protected
def local_request?
false
end
def rescue_action_in_public(exception)
logger.info("**** LOL ****")
redirect_to :root, :notice => "lol"
end
但是,触发ActiveRecord::RecordNotFound错误仍然显示堆栈跟踪,并且不会在我的日志中放置任何内容。
...接下来我读到你必须处于生产模式...所以我尝试了..同样的事情..
然后我读到,您需要替换local_request吗?在应用程序 Controller 中使用config / environments / production.rb中的方法
class ActionDispatch::Request
def local_request?(*args)
false
end
end
所以我做到了..仍然获得了堆栈跟踪,并且没有日志消息..
然后,我读到rescue_action_in_public必须在ShowExceptions类中。因此我将其放在config / initializers / error_handling.rb中。
module ActionDispatch
class ShowExceptions
protected
def rescue_action_in_public(exception)
logger.info("************** WTF ")
status = status_code(exception).to_s
template = ActionView::Base.new(["#{Rails.root}/app/views"])
if ["404"].include?(status)
file = "/errors/404.html.erb"
else
file = "/errors/500.html.erb"
end
body = template.render(:file => file)
render(status, body)
end
end
end
仍然..我得到了堆栈跟踪..仍然没有任何日志。
最佳答案
只需在生产模式下运行该应用程序:
$ rails server -e production
一切都应该正常工作。错误也记录在
log/production.log
中(仅在停止服务器后才会显示日志)。为了使一切正常,您可能首先需要使用
$ rake assets:precompile
并改变
config.serve_static_assets = true
在您的
config/environments/production.rb
中。
关于ruby-on-rails - super 沮丧关于result_action_in_public,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7354479/