我制作了一个简单的应用程序,我想测试页面是否有 404、500 等 http 错误。我已将 enviroments/development.rb 中的 config.consider_all_requests_local 更改为 false,但仍然遇到一些问题,所以我想问您几个问题...
如果我在浏览器中输入不适当的内容,例如
http://localhost:3000/products/dfgdgdgdgdgfd
,我仍然会看到旧的“未知操作”网站。但是,如果我输入我的计算机的本地 IP 地址。http://192.168.1.106:3000/products/dfgdgdgdgfd
我可以从公共(public)文件夹中看到404错误页面。为什么会发生这种情况?我知道,如果我将我的小项目部署在某个地方,我的应用程序将使用生产模式,如果发生任何错误,404 或 500 页面将显示。但是,如果我想让这些错误页面更加动态(例如,在使用包含流行产品列表的布局时呈现错误消息)或者只是将它们重定向到主页,该怎么办?
2.1。我找到的第一个解决方案是在应用程序 Controller 中使用rescue_from方法:
unless Rails.application.config.consider_all_requests_local
rescue_from Exception, :with => :render_error
rescue_from ActiveRecord::RecordNotFound, :with => :render_not_found
rescue_from AbstractController::ActionNotFound, :with => :render_not_found
rescue_from ActionController::RoutingError, :with => :render_not_found
rescue_from ActionController::UnknownController, :with => :render_not_found
rescue_from ActionController::UnknownAction, :with => :render_not_found
end
.
.
.
private
def render_error exception
Rails.logger.error(exception)
redirect_to root_path
#or
# render :controller=>'errors', :action=>'error_500', :status=>500
end
def render_not_found exception
Rails.logger.error(exception)
redirect_to root_path
#or
# render :controller=>'errors', :action=>'error_404', :status=>404
end
...但是该代码在任何情况下都不起作用。
2.2。第二种解决方案是放置 match "*path", :to => "products#show", :id=>1
(这是我愚蠢的应用程序中的示例主页)或 match "*path", :to => "errors#error_404", :id=>1
位于 paths.rb 文件末尾。该代码仅适用于像 http://192.168.1.106:3000/dfgdgdgdgdgfd
这样的拼写错误,因为如果我尝试 http://192.168.1.106:3000/products/dfgdgdgdgfd
( Controller 存在,但未找到操作)我仍然收到 404 页面。
我玩过一些尝试,例如 match "*path/*act"、 :to => "products#show", :id=>1
或 match ":controller(/*act)", :to => "products#show", :id=>8
但这也不起作用...
2.3。第三种解决方案是使用以下代码为错误创建 Controller 并在初始化器文件夹中创建一个文件:
# initializers/error_pages.rb
module ActionDispatch
class ShowExceptions
protected
def rescue_action_in_public(exception)
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
这非常有用,因为它可以让我渲染动态 erb 文件,但是..它不会渲染任何布局。我尝试将 body = template.render(:file => file)
更改为 body = template.render(:partial => file, :layout => "layouts/application")
但这只是表弟错误。
我知道我做错了,我相信这些错误页面有一个可行的解决方案,所以我希望您能提供帮助......
干杯。
最佳答案
在您的应用程序 Controller 中,您需要重写此方法:
def method_missing(m, *args, &block)
Rails.logger.error(m)
redirect_to :controller=>"errors", :action=>"error_404"
# or render/redirect_to somewhere else
end
然后你必须将它与此代码结合起来:
unless Rails.application.config.consider_all_requests_local
rescue_from Exception, :with => :method_missing
rescue_from ActiveRecord::RecordNotFound, :with => :method_missing
rescue_from AbstractController::ActionNotFound, :with => :method_missing
rescue_from ActionController::RoutingError, :with => :method_missing
rescue_from ActionController::UnknownController, :with => :method_missing
rescue_from ActionController::UnknownAction, :with => :method_missing
end
关于development-environment - Rails、开发环境和错误页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5912100/