Forum: Ruby on Rails Default routes for unknown 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.
E51c6ae07e72cf04ef869868cb8eca6e?d=identicon&s=25 Jake Janovetz (Guest)
on 2005-12-18 04:12
Hi folks-

I have the following map for default 'junk' routes:

  map.connect '*anything', :controller => 'welcome', :action =>
'unknown'

Which works just fine for a URL like: "mysite.com/junkjunkjunk"

However, it still tries to resolve an action when I do:
"mysite.com/my_controller/junkjunkjunk"

How can I send Rails to a default route in the case of an unknown
action?  Currently, I get the standard "Unknown action...  No action
responded to junkjunkjunk"

   Jake
88d3bfc2c18b9e73d9180aaac8c1b321?d=identicon&s=25 Patrice Neff (Guest)
on 2005-12-20 08:08
(Received via mailing list)
Jake Janovetz wrote:

> I have the following map for default 'junk' routes:
>
>   map.connect '*anything', :controller => 'welcome', :action =>
> 'unknown'
>
> Which works just fine for a URL like: "mysite.com/junkjunkjunk"
>
> However, it still tries to resolve an action when I do:
> "mysite.com/my_controller/junkjunkjunk"

Please show the complete routes setup, so we can help you.

> How can I send Rails to a default route in the case of an unknown
> action?  Currently, I get the standard "Unknown action...  No action
> responded to junkjunkjunk"

It should work if you put the following action in your application
controller. Please read the documentation for rescue_action_in_public
before returning with questions (because this method is not invoked
for local clients).

   def rescue_action_in_public(exception)
     case exception
     when ActionController::UnknownAction
       render :file => "#{RAILS_ROOT}/public/404.html", :status => 404
     else
       logger.error "Error raised: " + exception.to_s
       render :file => "#{RAILS_ROOT}/public/500.html",  :status => 500
     end
   end

I actually also redirect to 404 when a ActiveRecord::RecordNotFound
exception is encountered.

Regards,
Patrice
E51c6ae07e72cf04ef869868cb8eca6e?d=identicon&s=25 Jake Janovetz (Guest)
on 2005-12-20 16:39
>    def rescue_action_in_public(exception)
>      case exception
>      when ActionController::UnknownAction
>        render :file => "#{RAILS_ROOT}/public/404.html", :status => 404
>      else
>        logger.error "Error raised: " + exception.to_s
>        render :file => "#{RAILS_ROOT}/public/500.html",  :status => 500
>      end
>    end


Ah.  That explains a lot.  Thanks.  I didn't even know this function
existed.  The hard part is rarely the search.  It's knowing what to
search for.

   Jake
This topic is locked and can not be replied to.