Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails wildcard routes : vs *

I'm starting to learn rails, and I'm seeing the terminology wildcard routes, but I've seen routes listed both of the following ways:

/a/path/*all', :all => /.*/

and

/a/path/:all

What is the difference between these two route forms?

like image 298
Jeff Storey Avatar asked Jul 30 '12 23:07

Jeff Storey


People also ask

What does wildcard * * In a route path represent?

A Wildcard route has a path consisting of two asterisks (**). It matches every URL, the router will select this route if it can't match a route earlier in the configuration. A Wildcard Route can navigate to a custom component or can redirect to an existing route.

How many types of routes are there in Rails?

Rails RESTful Design which creates seven routes all mapping to the user controller. Rails also allows you to define multiple resources in one line.

How do Rails routes work?

Rails routing is a two-way piece of machinery – rather as if you could turn trees into paper, and then turn paper back into trees. Specifically, it both connects incoming HTTP requests to the code in your application's controllers, and helps you generate URLs without having to hard-code them as strings.

How do I see all routes in Rails?

Decoding the http request 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.


1 Answers

Have you read the Rails Guide on routing yet? That is a great place to start learning about routing in Rails.

For instance, you will learn that your 2nd code block is not a wildcard route. Instead it matches what the guide above refers to as a Static Segment

You'll also learn that to impose restrictions on a segment as you appear to be attempting in the first code block, you must use the :constraints option, such as this wildcard route, or as the guide above refers to them, Route Globbing

GET  "/a/path/*all", :constraints => { :all => /.*/ }

However, the above constraint is redundant since the wildcard *all is going to match .* by default anyway.

like image 144
deefour Avatar answered Oct 14 '22 08:10

deefour