Rails 4:如何创建使用资产管道的自定义404页面? [英] Rails 4: How do I create a custom 404 page that uses the asset pipeline?

查看:234
本文介绍了Rails 4:如何创建使用资产管道的自定义404页面?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有许多解决方案用于创建自定义的错误处理页面,但几乎没有Rails 4:





鼓励人们在 / public 中修改 404.html 的标准答案不适用于因为我想使用位于资产管道中的CSS主题。 有没有一种方法可以让html文件访问资产管道中定义的样式?如果没有,是否有一种创建可访问管道的自定义错误处理程序的方法?

解决方案

对于Rails 4.1,我喜欢这个答案,添加资产类型更好;但是我没有尝试过。在Rails 4.0.8中,这三个引用帮助了我:


  1. 动态错误页面是问题的第二个参考。这对我来说很好。


  2. 自定义错误页面可能已经从第一个引用或其他方式附带,但是通过添加有关Capybara测试的一些信息。


  3. 我没有进行Capybara测试,因为我不想更改测试配置;然而, RSpec-Rails请求规格使我能够独立测试这些请求,看到他们完成并返回正确的内容。


接下来是简要介绍三参考:


  1. 将以下设置添加到 config / environments / production.rb

     #将异常路由到应用程序路由器与默认
    config.exceptions_app = self.routes


  2. 编辑路由配置 config / routes.rb 将错误页面引导到错误控制器

     #错误页
    %w(404 422 500 503) .each do | code |
    获取代码,:to => errors#show,:code =>代码
    end

    将404,422,500和503页面请求路由到显示操作错误控制器与参数代码具有状态代码的值。


  3. 创建控制器 app / controllers / errors_controller.rb 。以下是全部内容:

      class ErrorsController< ApplicationController 

    def show
    status_code = params [:code] || 500
    flash.alert =状态#{status_code}
    呈现status_code.to_s,状态:status_code
    结束

    结束

    我的偏好是在 flash.alert

    上设置状态消息

  4. 自己创建页面。我使用 .erb 这里是 app / views / errors / 500.html.erb

     < p>我们的道歉。您的请求导致错误。< / p> 
    <%= render'product_description'%>

    所以你看到你可以渲染一个部分。该页面与所有布局样板从 app / views / layouts / application.html.erb 或您已配置的任何其他布局样板生成。这包括从闪存显示状态消息的< div id ='alert'><%= alert%>< / div> p>


  5. 通过添加测试文件 spec / requests / errors_request_spec.rb ,通过RSpec进行测试。以下是该文件的缩写内容,显示500状态页面的测试:

      require'rails_helper'

    RSpec.describeerrors,:type => :request do

    it显示500页do
    get/ 500
    assert_select'div#alert','状态500'
    assert_select'div [itemtype]'
    end

    end

    第一个断言检查闪光警报。第二个断言检查部分。



There are many solutions for creating customized error handling pages, but almost none for Rails 4:

The standard answer of encouraging people to modify 404.html in /public doesn't work for me because I want to use the CSS theme that resides in the asset pipeline. Is there a way that html files can access those styles defined in the asset pipeline? If not, is there a way to create a custom error handler that has access to the pipeline?

解决方案

For Rails 4.1 I like this answer, add an asset type better; however I have not tried it. On Rails 4.0.8, these three references helped me:

  1. Dynamic error pages is the second reference in the question. This worked just fine for me.

  2. Custom error pages may have cribbed from the first reference, or the other way around, but goes the extra mile by adding some information about testing with Capybara.

  3. I did not do the Capybara testing because I didn't want to change the test configuration; however, RSpec-Rails Request Specs clued me in to test these requests independently and see that they complete and return the correct content.

What follows is a nutshell description of what is taught by the three references:

  1. Add the following setting to config/environments/production.rb

    # Route exceptions to the application router vs. default
    config.exceptions_app = self.routes
    

  2. Edit the routing configuration, config/routes.rb to direct the error pages to an errors controller

      # error pages
      %w( 404 422 500 503 ).each do |code|
        get code, :to => "errors#show", :code => code
      end
    

    will route the 404, 422, 500, and 503 page requests to the show action of the errors controller with a parameter code that has the value of the status code.

  3. Create the controller, app/controllers/errors_controller.rb. Here is the entire content:

    class ErrorsController < ApplicationController
    
      def show
        status_code = params[:code] || 500
        flash.alert = "Status #{status_code}"
        render status_code.to_s, status: status_code
      end
    
    end
    

    My preference was to set a status message on flash.alert

  4. Create the pages themselves. I use .erb Here is app/views/errors/500.html.erb

    <p>Our apology.  Your request caused an error.</p>
    <%= render 'product_description' %>
    

    So you see that you can render a partial. The page renders with all of the layout boilerplate from app/views/layouts/application.html.erb or any other layout boilerplate that you have configured. That includes the <div id='alert'><%= alert %></div> that displays the status message from the flash.

  5. Tested with RSpec by adding a test file, spec/requests/errors_request_spec.rb. Here is abbreviated content of that file that shows a test of the 500 status page:

    require 'rails_helper'
    
    RSpec.describe "errors", :type => :request do
    
      it "displays the 500 page" do
        get "/500"
        assert_select 'div#alert', 'Status 500'
        assert_select 'div[itemtype]'
      end
    
    end
    

    The first assertion checks for the flash alert. The second assertion checks for the partial.

这篇关于Rails 4:如何创建使用资产管道的自定义404页面?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆