Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 如何在Rails中实现动态404500等错误页面?_Ruby On Rails_Ruby - Fatal编程技术网

Ruby on rails 如何在Rails中实现动态404500等错误页面?

Ruby on rails 如何在Rails中实现动态404500等错误页面?,ruby-on-rails,ruby,Ruby On Rails,Ruby,如何在Rails中实现动态的自定义错误页面 例如,使用application.html.erb布局和页面中的一些动态字段的自定义404错误页面 另外,如何从本地机器上测试这一点?请检查。其他的也可以通过谷歌找到 背后的想法是:Rails似乎在为404错误渲染public/404.html 如果要显示静态字段,则可以覆盖页面 对于动态内容,您似乎可以覆盖一个框架方法来钩住并重定向以呈现动态页面 ActionController::Rescue在公共中定义一个调用render\u optiona

如何在Rails中实现动态的自定义错误页面

例如,使用application.html.erb布局和页面中的一些动态字段的自定义404错误页面

另外,如何从本地机器上测试这一点?

请检查。其他的也可以通过谷歌找到

背后的想法是:Rails似乎在为404错误渲染
public/404.html

  • 如果要显示静态字段,则可以覆盖页面
  • 对于动态内容,您似乎可以覆盖一个框架方法来钩住并重定向以呈现动态页面

ActionController::Rescue
在公共中定义一个调用
render\u optional\u error\u文件的
Rescue\u action\u

  rescue_from ActiveRecord::RecordNotFound, :with => :render_record_not_found

  # Catch record not found for Active Record
  def render_record_not_found
    render :template => "shared/catchmissingpage", :layout => false, :status => 404
  end

  # Catches any missing methods and calls the general render_missing_page method
  def method_missing(*args)
    render_missing_page # calls my common 404 rendering method
  end

  # General method to render a 404
  def render_missing_page
    render :template => "shared/catchmissingpage", :layout => false, :status => 404
  end
def render_optional_error_file(status_code)
  status = interpret_status(status_code)
  render :template => "/errors/#{status.to_s[0,3]}.html.haml", :status => status, :layout => 'application.html.haml' if [404, 422, 500].include?(status)
  render :template => "/errors/unknown.html.haml", :status => status, :layout => 'application.html.haml' unless [404, 422, 500].include?(status)
end
您可以自定义渲染调用(使用模板、使用布局等)并通过这种方式捕获错误。现在,它捕捉到缺少的方法并记录未找到的方法,但可能在某些情况下,您希望显示一个500错误页面,这样您就可以继续,使用这种方法并使其适合您

对于从本地机器进行测试,它就是这样工作的。如果您只希望它在生产模式下工作,请添加

 if ENV['RAILS_ENV'] == 'production'

你很好。

如果你决定创建一个动态404(或其他状态代码)页面,请务必从
/public
(如果存在)中删除相应的html文件。

我在谷歌上看了一些关于如何实现这一点的博文,不幸的是,大多数人似乎依赖于污染你的应用程序控制器

我所做的是创建一个带有404消息的模板,然后使用该模板从rake任务更新public/404.html文件:

# Rake file to generate static 404 page

file "public/404.html" => ["app/views/layouts/application.html.erb"] do |t|
    print "Updating 404 page\n"
    `curl --silent http://locahost/content/error404 -o public/404.html`
end 

现在,每当我更新我的全局布局时,404页面就会自动更新。

在测试方面,一个真正好的方法是使用Passenger并将rails环境设置为production(或在站点配置中注释掉“RailsEnv development”)。至少通过这种方式,您可以模拟它在生产中的工作方式

但是,为了做到这一点,我有各种各样的设置文件,它们在启动时被解析,并根据环境进行拾取。其中一个设置是是否显示错误页面(AppSettings.show\u page\u errors?)。然后在我的应用程序控制器中

  if !AppSettings.show_page_errors?
    alias_method :rescue_action_locally, :rescue_action_in_public
  end
所以,它通常设置为默认设置,但有时我真的需要看看到底发生了什么,这样我就可以在生产时关闭它

另一个步骤是使用自定义页面。在我的例子中,我有基于错误的模板,其中还包括一个要提交到google forms的表单(因为我的服务器可能坏了)。为此,请将以下内容(并根据需要进行更改)放入应用程序控制器:

  rescue_from ActiveRecord::RecordNotFound, :with => :render_record_not_found

  # Catch record not found for Active Record
  def render_record_not_found
    render :template => "shared/catchmissingpage", :layout => false, :status => 404
  end

  # Catches any missing methods and calls the general render_missing_page method
  def method_missing(*args)
    render_missing_page # calls my common 404 rendering method
  end

  # General method to render a 404
  def render_missing_page
    render :template => "shared/catchmissingpage", :layout => false, :status => 404
  end
def render_optional_error_file(status_code)
  status = interpret_status(status_code)
  render :template => "/errors/#{status.to_s[0,3]}.html.haml", :status => status, :layout => 'application.html.haml' if [404, 422, 500].include?(status)
  render :template => "/errors/unknown.html.haml", :status => status, :layout => 'application.html.haml' unless [404, 422, 500].include?(status)
end

这将使用模板呈现状态代码404、422和500,但在其他情况下使用未知。如果您需要处理其他问题,只需更新此方法并添加模板。

@AnApp可能是。。这是一个旧答案-您可以将Rails 3版本作为一个单独的答案发布。该链接指向Nginx 404页面。:-)新的链接在Rails3.1上,对于一般的404页面,这不适用于我,只适用于找不到的记录。相反,它会继续从public/404.html中提取,如果该文件丢失,它只会呈现一个空页面。