1

2 つのセクションを持つ Rails アプリケーションが 1 つあるため、エラー ページに 2 つの異なるレイアウトを使用したいと考えています。

たとえば、エラーがセクション 1 から発生している場合、レイアウト 1 / 別のページをエラー (404、500) に使用する必要があります。

エラーがセクション 2 から発生している場合は、レイアウト 2 / 別のページをエラー (404、500) に使用する必要があります。

エラーページを定義するコードを書き、erb と ruby​​ コードで有効にしました。

application.rbで

config.exceptions_app = self.routes

routes.rb で

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

2 に答える 2

1

更新しました

少し考えてみました。エラーの種類が少ない場合は、このようにしてみてはいかがでしょうか。

最後の行routes.rbに、

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

これは、定義されていない (通常は をスローするActionController::RoutingError) パスと一致し、それをグローバル エラー ページにプッシュする必要があります。

上記のセグメントワイルドカードを使って遊んで、正しいパスを取得できます。これは、のような事前定義されたパスには影響しませんmydomain.com/controller1

以下は、よりきめ細かい制御方法です。

これにより、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

リファラーを使用してもうまくいかないようです。昨日の悪い回答で申し訳ありません。

于 2012-07-24T15:31:54.690 に答える
0

エラーコントローラーでは、誰がリファラーであるかをチェックし、それに基づいて条件付きレイアウトを作成できます

于 2012-07-24T14:45:55.620 に答える