Forum: Ruby on Rails Using the 404 for Missing Routes and Actions

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
346fbd85ecda084fc0757e14f60b564b?d=identicon&s=25 blaine (Guest)
on 2007-07-29 21:51
(Received via mailing list)
I've tried searching the list for this but I'm not really sure what
Rails would call it.  Basically what I'm wanting to do is to help
against users changing the URL string, or to help users with expired
bookmarks from the previous version of the site.  When a method
doesn't exist or an action on a method rather then showing "Routing
Error" or "Unknown Action" I want to be able to either forward that to
the 404 error page or just redirect it to my home controller's index
action.  How would one go about doing this?

Regards,

Tim
53a92d87e6de7031c253de28acc46971?d=identicon&s=25 Stan (Guest)
on 2007-07-31 02:05
(Received via mailing list)
There may be a more elegant way, but this works:

Create a controller with an action that returns a 404 (or an action
that would redirect to your home page whatever that is or whatever
else you want to happen)

class ErrorsController < ApplicationController
  def not_found
    render :file => "#{RAILS_ROOT}/public/404.html", :layout =>
false, :status => 404
  end
end

then put a catch-all route to this at the end of /config/routes.rb:

ActionController::Routing::Routes.draw do |map|
  # snip
  map.connect '*path', :controller => 'errors', :action => 'not_found'
end

Any input that a miscreant user enters that doesn't match a route will
be caught by this final route and cause the not_found action to fire.
821395fe70906c8290df7f18ac4ac6cf?d=identicon&s=25 Rick Olson (Guest)
on 2007-07-31 03:08
(Received via mailing list)
On 7/29/07, blaine <jangchub@gmail.com> wrote:
>
> I've tried searching the list for this but I'm not really sure what
> Rails would call it.  Basically what I'm wanting to do is to help
> against users changing the URL string, or to help users with expired
> bookmarks from the previous version of the site.  When a method
> doesn't exist or an action on a method rather then showing "Routing
> Error" or "Unknown Action" I want to be able to either forward that to
> the 404 error page or just redirect it to my home controller's index
> action.  How would one go about doing this?

Those development stack traces are only shown in development mode, or
in production mode IF the request is from the local web server.  Look
at extending #rescue_action_in_public to suit your needs:

http://dev.rubyonrails.org/browser/branches/1-2-st...

I don't know if there are any decent tutorials written on it though.
But if there aren't, they should be written for edge which improves
this a lot IMO:

http://dev.rubyonrails.org/browser/trunk/actionpac...

You can easily define a status code and rendered template for custom
exceptions without overwriting the method.

--
Rick Olson
http://lighthouseapp.com
http://weblog.techno-weenie.net
http://mephistoblog.com
This topic is locked and can not be replied to.