Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails catch-all/globbing routes

I am using rails 3.0.14, and I am constructing routes.rb using the resourceful style. I'd like to have a wildcard route that catches all requests that do not match to any route stated.

What's the appropriate way to construct such a route?

like image 398
Evil Nodoer Avatar asked Nov 14 '12 22:11

Evil Nodoer


People also ask

How do I see all routes in Rails?

TIP: If you ever want to list all the routes of your application you can use rails routes on your terminal and if you want to list routes of a specific resource, you can use rails routes | grep hotel . This will list all the routes of Hotel.

What is match in Rails routes?

Rails routes are matched in the order they are specified, so if you have a resources :photos above a get 'photos/poll' the show action's route for the resources line will be matched before the get line. To fix this, move the get line above the resources line so that it is matched first.

What are RESTful routes in Rails?

In Rails, a RESTful route provides a mapping between HTTP verbs, controller actions, and (implicitly) CRUD operations in a database. A single entry in the routing file, such as. map.resources :photos. creates seven different routes in your application: HTTP verb.

What is the difference between resources and resource in Rails?

Difference between singular resource and resources in Rails routes. So far, we have been using resources to declare a resource. Rails also lets us declare a singular version of it using resource. Rails recommends us to use singular resource when we do not have an identifier.


2 Answers

put

match '*path' => 'your_controller#your_action' 

at the end of the routes.rb file. This is important, since the routes are stepped through top down.

See also http://guides.rubyonrails.org/routing.html -> 3.10

like image 99
awenkhh Avatar answered Oct 05 '22 09:10

awenkhh


For Rail 4, you need to specify the request type:

match "*path", to: "application#custom_action", via: :all 

As others have said, put this at the very bottom of your routes file.

like image 38
steel Avatar answered Oct 05 '22 08:10

steel