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?
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.
Rails RESTful Design which creates seven routes all mapping to the user controller. Rails also allows you to define multiple resources in one line.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With