如何在单个rails应用程序中为相同的错误代码(例如404)呈现两个不同的错误页面?

时间:2022-10-04 19:19:17

I have one rails application in which I have two sections, so I want to use two different layouts for the Error page.

我有一个rails应用程序,其中我有两个部分,所以我想为Error页面使用两个不同的布局。

For example, if an error is coming from Section 1 then layout1 / different page should be used for the Error (404, 500).

例如,如果错误来自第1节,则应使用layout1 /不同页面作为错误(404,500)。

If error is coming from Section 2 then layout2 / different page should be used for the Error (404, 500).

如果错误来自第2节,那么layout2 /不同的页面应该用于错误(404,500)。

I've written code to define the Error page, enabled with erb and ruby code.

我编写了代码来定义错误页面,启用了erb和ruby代码。

in application.rb

在application.rb中

config.exceptions_app = self.routes

in routes.rb

在routes.rb中

match "/404", :to => "errors#error_404"
match "/500", :to => "errors#error_500"

2 个解决方案

#1


1  

Updated

更新

Thought about it a little. If you only have a few types of errors, how about doing it like this?

想一想。如果您只有几种类型的错误,那么这样做怎么样?

In your routes.rb, at the very last line, add a

在您的routes.rb中,在最后一行添加一个

match '/my_segment/*path', :to => 'errors#not_found'

匹配'/ my_segment / * path',:to =>'errors#not_found'

This should match any path that is not defined (which normally throws ActionController::RoutingError) and push it to your global error page.

这应匹配任何未定义的路径(通常会抛出ActionController :: RoutingError)并将其推送到全局错误页面。

You can play with play with the segments wildcard above to get your correct path. This should NOT affect your predefined paths, like mydomain.com/controller1.

您可以使用上面的段通配符进行游戏,以获得正确的路径。这不应该影响您的预定义路径,例如mydomain.com/controller1。

Below is a more fine grained method of control.

下面是一种更细粒度的控制方法。

This will help you match any errors from mydomain.com/some_controller/bad_params

这将帮助您匹配mydomain.com/some_controller/bad_params中的任何错误

def firstController < ApplicationController 
  def method_in_first_controller
    # Do something here
    rescue
      @error = # Error object here
      render :template=>"some_error_template", :status => :not_found # In specific action
  end
end


def secondController < ApplicationController 
  rescue_from ActiveRecord::RecordNotFound, :with => :rescue_not_found # In secondController

  def method_in_second_controller 
    # Do something  
  end

  protected
  def rescue_not_found
    @error = # Error object here
    render :template => 'some_error_template', :status => :not_found
  end

end

def ApplicationController 
  rescue_from ActiveRecord::RecordNotFound, :with => :rescue_not_found # Globally

  protected
  def rescue_not_found
    @error = # Error object here
    render :template => 'application/not_found', :status => :not_found
  end
end

Using referrer doesn't seem to get anywhere, sorry for the bad answer yesterday.

使用推荐人似乎无处可去,对不起昨天的错误答案。

#2


0  

In your errors controller you can have a check who is the referrer and have a conditional layout based on that

在您的错误控制器中,您可以检查谁是引荐来源并具有基于此的条件布局

#1


1  

Updated

更新

Thought about it a little. If you only have a few types of errors, how about doing it like this?

想一想。如果您只有几种类型的错误,那么这样做怎么样?

In your routes.rb, at the very last line, add a

在您的routes.rb中,在最后一行添加一个

match '/my_segment/*path', :to => 'errors#not_found'

匹配'/ my_segment / * path',:to =>'errors#not_found'

This should match any path that is not defined (which normally throws ActionController::RoutingError) and push it to your global error page.

这应匹配任何未定义的路径(通常会抛出ActionController :: RoutingError)并将其推送到全局错误页面。

You can play with play with the segments wildcard above to get your correct path. This should NOT affect your predefined paths, like mydomain.com/controller1.

您可以使用上面的段通配符进行游戏,以获得正确的路径。这不应该影响您的预定义路径,例如mydomain.com/controller1。

Below is a more fine grained method of control.

下面是一种更细粒度的控制方法。

This will help you match any errors from mydomain.com/some_controller/bad_params

这将帮助您匹配mydomain.com/some_controller/bad_params中的任何错误

def firstController < ApplicationController 
  def method_in_first_controller
    # Do something here
    rescue
      @error = # Error object here
      render :template=>"some_error_template", :status => :not_found # In specific action
  end
end


def secondController < ApplicationController 
  rescue_from ActiveRecord::RecordNotFound, :with => :rescue_not_found # In secondController

  def method_in_second_controller 
    # Do something  
  end

  protected
  def rescue_not_found
    @error = # Error object here
    render :template => 'some_error_template', :status => :not_found
  end

end

def ApplicationController 
  rescue_from ActiveRecord::RecordNotFound, :with => :rescue_not_found # Globally

  protected
  def rescue_not_found
    @error = # Error object here
    render :template => 'application/not_found', :status => :not_found
  end
end

Using referrer doesn't seem to get anywhere, sorry for the bad answer yesterday.

使用推荐人似乎无处可去,对不起昨天的错误答案。

#2


0  

In your errors controller you can have a check who is the referrer and have a conditional layout based on that

在您的错误控制器中,您可以检查谁是引荐来源并具有基于此的条件布局