How to setup a 404 page? #23

Closed
frarees opened this Issue Sep 27, 2011 · 9 comments

Projects

None yet

6 participants

@frarees

I've been looking on how to set this up to render a page when a template is not found. Can anyone help me with this? Thank you all.

@mjankowski
thoughtbot, inc. member

So when someone requests a non-existent page that hits the high voltage controller, you want to render something other than the 404.html page?

@frarees

When it doesn't find the page, it renders a message like: Cannot find page: /page/foo

I want it to render my custom 404 page, but I don't get a clue about how to do it.

@mjankowski
thoughtbot, inc. member

If you look here, you can see what it's doing - https://github.com/thoughtbot/high_voltage/blob/master/app/controllers/high_voltage/pages_controller.rb#L5-11

If you try to handle an ActionController::RoutingError in your custom 404, will that work?

@mjankowski mjankowski closed this Oct 12, 2011
@dormi

So, if i want to get 404.html served by high_voltage, I need a custom PagesController?

I would like to remove public/404.html and add /app/pages/404.hml, but I underestand that doesn't work like this.

Am I right?

@mjankowski
thoughtbot, inc. member

Rendering a dynamic 404 page in rails is not necessarily related to high_voltage, if that's what you want. Check out the rescue_from method - http://api.rubyonrails.org/classes/ActiveSupport/Rescuable/ClassMethods.html#method-i-rescue_from - that might help.

If you ARE trying to add 404 customization to high_voltage, can you be more specific about what you want to do?

@Godisemo

Since high_voltage is an engine there seems to be a problem using the normal rescue_frommethod. Therefor it would be nice to at least have the option to have a default page which would be rendered when no page was matched.

http://stackoverflow.com/questions/6902970/how-to-override-rails-3-1-routing-error-when-using-engines-with-glob-routes

this gem seems to fix the problem https://github.com/vidibus/vidibus-routing_error

@dormi

@mjankowski, what i was trying to do was use high_voltage to render a dynamic 404.

As you say, this can be achieved without high_voltage, this is what I have in application_controller.rb:

unless config.consider_all_requests_local
  rescue_from Exception, :with => :render_500
  rescue_from ActiveRecord::RecordNotFound, :with => :render_404
  rescue_from ActionController::RoutingError, :with => :render_404
  rescue_from ActionController::UnknownController, :with => :render_404
  rescue_from ActionController::UnknownAction, :with => :render_404
end

protected

def render_500 exception
  logger.error exception.inspect
  ExceptionMailer.exception(exception).deliver
  render :template => "/pages/500.html.erb", :status => 500, :layout => 'static'
end

def render_404 exception
  logger.error exception.inspect
  render :template => "/pages/404.html.erb", :status => 404, :layout => 'static'
end

Thank you!

@oldtinroof

Sorry to ressurect such an old issue, but I've tried to implement this the way @dormi suggests and HV doesn't seem to catch the "rescue_from ActionController::RoutingError, :with => :render_404" in my application controller.

I get the vanilla error page with page not found.

I've also tried the suggestion from @Godisemo and the vidibus page about using the config.exceptions_app = self.routes trick - but that doesn't work at all.

I'm on Rails 3.2.11 and HV 1.2.2...

@yevgenko

It is depend on particular use case, but in case someone looking to simply handle invalid pages:

class PagesController < ApplicationController
  include HighVoltage::StaticPage

  def invalid_page
    render('pages/404-error', :status => 404)
  end
end

Please see "Override" scection for more details about PagesController:
https://github.com/thoughtbot/high_voltage#override

p.s.
invalid_page is the method from HighVoltage::StaticPage module, which, by default, raises ActionController::RoutingError https://github.com/thoughtbot/high_voltage/blob/54217603f6745a1feecff6761bc600dc07631ba4/app/controllers/concerns/high_voltage/static_page.rb#L22-L24

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment