简体   繁体   English

Rails:如何在某个状态代码上显示页面?

[英]Rails: How can I display a page on a certain status code?

I'm relatively new to Rails, and I still don't know a lot about routing. 我对Rails比较陌生,但是对路由我还是不太了解。 How would I display a certain view (such as 404.html.erb ) when the user gets a certain error code (404)? 当用户收到某个错误代码(404)时,如何显示某个视图(例如404.html.erb )? For example, if a user GETs a page that doesn't exist (404), how can I listen for that and display a certain page on that event? 例如,如果用户获取一个不存在的页面(404),我该如何监听该事件并在该事件上显示某个页面?

I know that a Rails app comes with a public folder and pre-generated 404, 500, and 422 error code .html pages, but they don't seem to work for me. 我知道,一个Rails应用程序带有一个public文件夹,并预先生成404,500,和422错误代码的.html页面,但他们似乎并没有为我工作。 When I go to a page that doesn't exist, I receive a GET error. 当我转到一个不存在的页面时,我收到一个GET错误。 How would I change this? 我将如何改变呢?

Thanks! 谢谢!

You can setup custom routes to render dynamic pages from your controller just as normal controller-view templates. 您可以设置自定义路由,以像常规控制器视图模板一样从控制器呈现动态页面。

config/routes.rb 配置/ 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

app/controllers/errors_controller.rb 应用程序/控制器/ errors_controller.rb

class ErrorsController < ApplicationController    
  def not_found
    render(:status => 404)
  end
end

app/views/errors/not_found.html.erb 应用程序/视图/错误/ not_found.html.erb

<div class="container">
  <div class="align-center">
    <h3>Page not found</h3>
    <%= link_to "Go to homepage", root_path %>
  </div>
</div>

You can access the public pages through the localhost. 您可以通过本地主机访问公共页面。 Rails automatically uses those pages when it encounters that error or 404 on production. 当遇到错误或生产中出现404时,Rails会自动使用这些页面。

localhost:3000/404 # => renders the 404.html
localhost:3000/500 # => renders the 500.html

You can do as below: 您可以执行以下操作:

# 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

Restart server after those changes. 这些更改后,请重新启动服务器。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在特定表格中显示特定属性,而不为当前用户显示所有属性RAILS 4 - How can i display a certain attribute in a certain table without displaying all for a current user RAILS 4 如何根据 Rails 中的某些条件显示男性或女性? - How do I display male or female based on certain conditions in Rails? 如何在我正在查看的页面上显示项目数? (在rails / shopify上使用液体/红宝石) - How I can display the number of items on the page I am looking at? (using liquid / ruby on rails / shopify) 如何在Rails中使用SQL代码对此进行编码? - How can I code this with SQL code in rails? 如何在.html.erb页面上显示控制器的变量(循环中)? 红宝石在铁轨上 - how can I display controller's variable (which is on a loop) on .html.erb page? ruby on rails 在用Rails提交红宝石表单之前,如何在页面上显示开放图协议的数据? - How can I display data of an open graph protocol on a page before a form is submitted in ruby on rails? 如何使用Rails在HTML页面上显示所有环境变量? - How can I display all the environment variables on an HTML page using Rails? 在任何失败情况下,如何使用Ruby on Rails显示自定义错误页面? - How can I display a custom error page with Ruby on Rails in any failure case? 我如何在Rails中显示Ajax错误 - How can i display an error for ajax in rails 如何从Rails页面中请求Rails页面? - How can I request a Rails page from within a Rails page?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM