1

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

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

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

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

in application.rb

config.exceptions_app = self.routes

in routes.rb

match "/404", :to => "errors#error_404"
match "/500", :to => "errors#error_500"
1
  • Hi, I updated my answer. Hopefully it helps to solve your question. Commented Jul 25, 2012 at 2:48

2 Answers 2

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

match '/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.

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.

Below is a more fine grained method of control.

This will help you match any errors from 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.

Sign up to request clarification or add additional context in comments.

Comments

0

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

1 Comment

how can I check the referrer ? Is there any specific code for this?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.