Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/59.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:如何显示特定状态代码的页面?_Ruby On Rails_Url Routing - Fatal编程技术网

Ruby on rails Rails:如何显示特定状态代码的页面?

Ruby on rails Rails:如何显示特定状态代码的页面?,ruby-on-rails,url-routing,Ruby On Rails,Url Routing,我对Rails还比较陌生,但我仍然不太了解路由。当用户收到某个错误代码(404)时,如何显示某个视图(如404.html.erb)?例如,如果用户获得一个不存在的页面(404),我如何监听该页面并在该事件上显示特定页面? 我知道Rails应用程序附带了一个public文件夹和预生成的404、500和422错误代码.html页面,但它们似乎不适合我。当我转到一个不存在的页面时,我收到一个GET错误。我将如何更改此设置? 谢谢 您可以通过本地主机访问公共页面。Rails在生产中遇到错误或404时会自

我对Rails还比较陌生,但我仍然不太了解路由。当用户收到某个错误代码(404)时,如何显示某个视图(如
404.html.erb
)?例如,如果用户获得一个不存在的页面(404),我如何监听该页面并在该事件上显示特定页面?

我知道Rails应用程序附带了一个
public
文件夹和预生成的404、500和422错误代码.html页面,但它们似乎不适合我。当我转到一个不存在的页面时,我收到一个GET错误。我将如何更改此设置?


谢谢

您可以通过本地主机访问公共页面。Rails在生产中遇到错误或404时会自动使用这些页面

localhost:3000/404 # => renders the 404.html
localhost:3000/500 # => renders the 500.html
您可以执行以下操作:

# in config/application.rb
config.exceptions_app = self.routes

# For testing on development environment, need to change consider_all_requests_local
# in config/development.rb
config.consider_all_requests_local = false
在这些更改之后重新启动服务器

# in routes.rb
get '/404', to: 'errors#not-found'
get '/422', to: 'errors#unprocessable-entity'
get '/500', to: 'errors#internal-error'

您可以设置自定义路由以从控制器呈现动态页面,就像普通控制器视图模板一样

config/routes.rb

MyApp::Application.routes.draw do
  # custom error routes
  match '/404' => 'errors#not_found', :via => :all
  match '/500' => 'errors#internal_error', :via => :all
end
class ErrorsController < ApplicationController    
  def not_found
    render(:status => 404)
  end
end
<div class="container">
  <div class="align-center">
    <h3>Page not found</h3>
    <%= link_to "Go to homepage", root_path %>
  </div>
</div>
app/controllers/errors\u controller.rb

MyApp::Application.routes.draw do
  # custom error routes
  match '/404' => 'errors#not_found', :via => :all
  match '/500' => 'errors#internal_error', :via => :all
end
class ErrorsController < ApplicationController    
  def not_found
    render(:status => 404)
  end
end
<div class="container">
  <div class="align-center">
    <h3>Page not found</h3>
    <%= link_to "Go to homepage", root_path %>
  </div>
</div>
类错误控制器404)
终止
终止
app/views/errors/not_found.html.erb

MyApp::Application.routes.draw do
  # custom error routes
  match '/404' => 'errors#not_found', :via => :all
  match '/500' => 'errors#internal_error', :via => :all
end
class ErrorsController < ApplicationController    
  def not_found
    render(:status => 404)
  end
end
<div class="container">
  <div class="align-center">
    <h3>Page not found</h3>
    <%= link_to "Go to homepage", root_path %>
  </div>
</div>

找不到页面