我每天都有错误的请求,而不是在production.log中生成大量日志,如下所示:
F, [2016-02-08T15:39:17.698761 #2437] FATAL -- :
ActionController::RoutingError (No route matches [GET] "/sites/default/files/elsevier/eop/S0025-7753(12)00231-X.pdf"):
actionpack (4.0.2) lib/action_dispatch/middleware/debug_exceptions.rb:21:in `call'
actionpack (4.0.2) lib/action_dispatch/middleware/show_exceptions.rb:30:in `call...'
在我的网站上没有/ sites /`的路由:这是一个外来的webservice错误,我想知道它的IP来解决问题。
我已经学会了这个,但是没有用,我想我还需要其他代码
(我有Rails 4.0.2,我很快就会升级到4.2.5。)
1-在routes.rb
,最后:
match '*path', :to => 'application#routing_error', via: :all
2-在application_ontroller
:我记录IP,然后我需要渲染404.html如果我正在制作或渲染轨道错误,如果我正在开发
def routing_error(error = 'Routing error', status = :not_found, exception=nil)
logger.debug { "---> I catch you #{request.remote_ip}" }
render status: 404, file: "404.html"
end
答案 0 :(得分:1)
解决方案:
在routes.rb
中class RoutingErrorConstrain
def initialize
@prodenv = Rails.env == "production"
end
def matches?(request)
@prodenv
end
end
MyApp::Application.routes.draw do
...
match '*path', to: 'application#render_routing_error',
via: :all,
constraints: RoutingErrorConstrain.new
...
end
在application_controller.rb中:
def render_routing_error
logger.warn do "
BAD IP: #{request.ip}. Remote_ip: #{request.remote_ip}"
end
respond_to do |format|
format.html
{ render file: "public/404.html", layout: false }
format.all { render nothing: true, status: status }
end
end
答案 1 :(得分:0)
在application_controller
中,只需写下以下代码 -
rescue_from ActionController::RoutingError, with: lambda { |exception| render_error(404, exception) }
def render_error(status, exception)
respond_to do |format|
format.html { render template: "public/#{status}", layout: false, status: status }
format.all { render nothing: true, status: status }
end
end
您可以在方法中添加条件来检查rails环境。
希望这会有所帮助!!