Reputation: 13581
I have a situation where I'm in need to constructing pretty url paths.
I have a FilesController that need to handle URLs like:
mydomain.com/files/path/dir1/dir2/user/bob
mydomain.com/files/path/dir1/user/bob
mydomain.com/files/path/dir1
mydomain.com/files/user/bob
In the controller, I want params[:path]
to contain everything between /path
and /user
and params[:user]
to contain anything after /user
(assuming only one user and it's optional).
I'm looking for the best way to do this, preferable with just one statement in the routes.rb
file. The trickiest part, I think, is that after /path
an actual path to a file might be provided, N times deep.
Upvotes: 3
Views: 3295
Reputation: 62698
You want route globbing:
match 'files/*path/user/:user' => 'user#files'
You might also need to add an additional route for the case where there is no path:
match 'files/user/:user' => 'user#files'
Upvotes: 4
Reputation: 13581
Thanks to Chris Heald's answer, I ended up going with this, which fit the scenario:
match 'files/user/:user' => 'files#index'
match 'files/path/*path/user/:user' => 'files#index'
match 'files/path/*path' => 'files#index'
Still wondering if this could've been handled with a one-liner.
Upvotes: 1