【问题标题】:Rails exception_handler gem not updating 404 pageRails exception_handler gem 未更新 404 页面
【发布时间】:2017-09-30 16:33:07
【问题描述】:

我正在使用 exception_handler gem 来制作自定义 404 页面。当我转到我网站上的 404 页面时,它显示未找到,但如何自定义此页面?我尝试更改路线,制作 errors_controller 和 not_found 视图。

application.rb

config.exceptions_app = self.routes

routes.rb

  get "/404", :to => "errors#not_found"
  get "/422", :to => "errors#unacceptable"
  get "/500", :to => "errors#internal_error"

errors_controller.rb

class ErrorsController < ApplicationController

  def not_found
    render :status => 404   end

  def unacceptable
    render :status => 422   end

  def internal_error
    render :status => 500   end

end

app/views/errors/not_found.html.erb

<label> TEST 404 Label </label>

【问题讨论】:

    标签: ruby-on-rails ruby exceptionhandler


    【解决方案1】:

    很有可能您已经正确配置了这些页面,并且代码可以正常工作,但不适用于您正在工作的环境。在目录config/environments 中,您应该有三个文件:development, test, production。当您在开发中工作时,它不会呈现您创建的那些页面,因为它默认为 Rails 错误。您应该签入config/environments/development.rb 并查看您将config.consider_all_requests_local 设置为什么。您可以将其更改为false,重置路由器并能够查看错误页面,然后在继续进一步开发时将其换回true。请注意,您使用的实现看起来像是 exception_handler gem 的替代方案,并且会处理错误而不是那个 gem。

    您可以在 Rails 指南中找到更多信息:http://guides.rubyonrails.org/configuring.html#rails-general-configuration

    【讨论】:

      【解决方案2】:

      我忘记运行 rails g exception_handler:views 这会在我的项目文件中生成所有视图,现在我可以根据自己的喜好自定义它们。

      【讨论】:

      • 我编写并维护了 gem - 如果您需要任何直接支持,欢迎发送电子邮件至 rpeck@frontlineutilities.co.uk
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-12-03
      • 1970-01-01
      • 2019-11-27
      • 1970-01-01
      • 2013-12-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多