user1859243
user1859243

Reputation: 315

How to create rails route with constrants for numbers-only that accepts empty parameter?

I have created some route like this:

get 'foo/:offset' => 'foo#action', :as => :foo, :constraint => { id: /\d+/ }

It works fine, but: I want rails to route /foo to foo#action if no parameter specified, so not only urls like /foo/123 will be routed but simple /foo too.

How can I change constraint for this? Thanks for help!

Upvotes: 0

Views: 105

Answers (1)

Crystian Leão
Crystian Leão

Reputation: 705

I don't know if this is the best solution, but it's a simple one:

Create a route for /foo, and another for /foo/:offset to the same controller and action!

Upvotes: 1

Related Questions