Saim
Saim

Reputation: 2523

Rails route with N number of parameters

I have application which allow users to create there on URL for a page on a particular domain.

What I want to achieve is to create URL having variable number of parameters separated by "/". For example:

www.mydomain.com/a

ww.mydomain.com/a/b

www.mydomain.com/a/b/c

and so on. After the root, everything would be considered as parameters. The final result I need in Rails code is two strings:

1."www.mydomain.com" 2. "a" or "a/b" or "a/b/c" (whatever is after the root)

Thanks, Imran

Upvotes: 1

Views: 1220

Answers (2)

Jeriko
Jeriko

Reputation: 6637

Sounds like you want a catch-all route. Add the following line to your routes.rb file:

map.connect '*path', :controller => 'your_controller' :action => 'your_action'

No matter what URL is supplied, the request path is captured and delimited by / into an array. You can access this via params[:path].

Since this will match any and every request, any other routes you have should be declared before this one.

Check out Ryan Bates' catch-all route railscast for more info.

Upvotes: 4

marcgg
marcgg

Reputation: 66445

You should look into the Rails Guide "Rails Routing from the Outside In ".

I'm not sure if I get your question, but it seems that you want something close to:

map.connect ':first_id/:second_id/:third_id'

Upvotes: 0

Related Questions