0
我试图从插件修补我的Rails(4.2.5)应用程序的ApplicationController。 我要添加 'rescue_from的ActiveRecord :: RecordNotFound' 我ApplicationController.Ways我已经试过至今:Rails将“rescue_from”方法从补丁//插件添加到应用程序控制器
1.
module ApplicationControllerPatch
def self.included(base)
base.class_eval do
rescue_from ActiveRecord::RecordNotFound do |e|
redirect_to root_path
end
end
end
end
ApplicationController.send(:include, ApplicationControllerPatch)
2.
module ApplicationControllerPatch
def self.included(base)
base.send(:include, InstanceMethods)
base.class_eval do
rescue_from ActiveRecord::RecordNotFound, with: :not_found
end
end
module InstanceMethods
def not_found
redirect_to root_path
end
end
end
ApplicationController.send(:include, ApplicationControllerPatch)
解在此堆栈溢出链接: How do I require ActiveSupport's rescue_from method?
直到现在,似乎没有任何方法可行。 请提供任何解决方案或帮助纠正上述代码中是否有错误。
只包含一个模块在ApplicationController中,而不是试图猴补丁它从外面。否则,你不得不担心monkeypatch执行的时间。 – max
我无法更改核心Rails应用程序中的代码,因此需要猴子补丁。 – user8544663