Ruby on rails 404状态:缺少布局

Ruby on rails 404状态:缺少布局,ruby-on-rails,ruby-on-rails-3,Ruby On Rails,Ruby On Rails 3,当找不到记录时,我呈现404页。问题是它没有应用程序布局,尽管403工作正常 class ApplicationController < ActionController::Base rescue_from ActiveRecord::RecordNotFound, with: :render_404 def render_404 render file: 'public/404.html', status: 404, layout: 'application' e

当找不到记录时,我呈现404页。问题是它没有
应用程序
布局,尽管
403
工作正常

class ApplicationController < ActionController::Base

  rescue_from ActiveRecord::RecordNotFound, with: :render_404

  def render_404
    render file: 'public/404.html', status: 404, layout: 'application'
  end

  def render_403
    render file: 'public/403.html', status: 403, layout: 'application'
  end

end
class ApplicationController
您确定正在执行您的自定义救援吗?。。我不这么认为。 可能会引发另一个异常,而不是
ActiveRecord::RecordNotFound

问题是,
public/404.html
默认情况下由rails为404错误呈现,没有布局。
如果您想调整此行为,请删除该
public/*
文件并将其放在
app/views
文件夹下,这样您就拥有了完全控制权,rails默认行为不会让您感到困惑。

我们有一种更好的捕获异常的方法:

(这是我的照片)


捕获

捕获异常的一种更有效的方法是使用该方法

--

过程

其次,您应该处理捕获的异常。您可以通过将请求发送到控制器方法(我们使用
ExceptionController\show
)来实现这一点:

#app/controllers/exception_controller.rb
类异常控制器<应用程序控制器
#回应
响应:html,:xml,:json
#依赖关系
行动前:状态
#布局
布局:布局状态
####################
#行动#
####################
#展示
def秀
以状态响应_:@status
结束
####################
#依赖关系#
####################
受保护的
#信息
def状态
@exception=env['action\u dispatch.exception']
@status=ActionDispatch::ExceptionWrapper.new(env,@exception).status\u代码
@response=ActionDispatch::ExceptionWrapper.rescue\u responses[@exception.class.name]
结束
#格式
def详细信息
@详细信息| |={}。点击do | h|
I18n.with_options作用域:[:exception,:show,@response],exception_name:@exception.class.name,exception_message:@exception.message do|I18n|
h[:name]=i18n.t“#{@exception.class.name.下划线}.title”,默认值:i18n.t(:title,默认值:@exception.class.name)
h[:message]=i18n.t“#{@exception.class.name.下划线}.description”,默认值:i18n.t(:description,默认值:@exception.message)
结束
结束
结束
方法:详细信息
####################
#布局#
####################
私有的
#布局
def布局_状态
@status.to_s==“404”?“应用程序”:“错误”
结束
结束
--

展示

最后,您可以输出收到的消息,每个错误都有自定义布局:

#app/views/exception/show.html.erb
<div class="box">
    <h1><%= details[:name] %></h1>
    <p><%= details[:message] %></p>
</div>
#app/views/exception/show.html.erb


你能告诉我它有什么布局吗?@RichPeck应该有
应用程序
两种状态,但是404我想我的原始观点仍然站得住脚——如果不是
应用程序
,它有什么布局?@RichPeck它没有任何布局。谢谢-我来看看为什么会出现这个问题
#app/controllers/exception_controller.rb
class ExceptionController < ApplicationController

  #Response
  respond_to :html, :xml, :json

  #Dependencies
  before_action :status

  #Layout
  layout :layout_status

  ####################
  #      Action      #
  ####################

  #Show
  def show
    respond_with status: @status
  end

  ####################
  #   Dependencies   #
  ####################

  protected

  #Info
  def status
    @exception  = env['action_dispatch.exception']
    @status     = ActionDispatch::ExceptionWrapper.new(env, @exception).status_code
    @response   = ActionDispatch::ExceptionWrapper.rescue_responses[@exception.class.name]
  end

  #Format
  def details
    @details ||= {}.tap do |h|
      I18n.with_options scope: [:exception, :show, @response], exception_name: @exception.class.name, exception_message: @exception.message do |i18n|
        h[:name]    = i18n.t "#{@exception.class.name.underscore}.title", default: i18n.t(:title, default: @exception.class.name)
        h[:message] = i18n.t "#{@exception.class.name.underscore}.description", default: i18n.t(:description, default: @exception.message)
      end
    end
  end
  helper_method :details

  ####################
  #      Layout      #
  ####################

  private

  #Layout
  def layout_status
    @status.to_s == "404" ? "application" : "error"
  end
end
#app/views/exception/show.html.erb
<div class="box">
    <h1><%= details[:name] %></h1>
    <p><%= details[:message] %></p>
</div>