具有多个,可选和漂亮参数的路由 [英] Routes with multiple, optional, and pretty parameters
问题描述
我遇到了需要构造漂亮的url路径的情况。
I have a situation where I'm in need to constructing pretty url paths.
我有一个FilesController,需要处理以下URL:
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
在控制器中,我希望 params [:path]
包含 / path
与 /用户
和 params [:user]
包含 / user $ c之后的任何内容$ c>(假设只有一个用户,它是可选的。)
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).
我正在寻找最好的方法,最好在<$ c中只声明一条$ c> routes.rb 文件。我认为最棘手的部分是,在 / path
之后,可以提供文件的实际路径,深度为N倍。
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.
推荐答案
您要路由通行:
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'
这篇关于具有多个,可选和漂亮参数的路由的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!