我遇到了需要构造漂亮的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/files/path/dir1/dir2/user/bob mydomain/files/path/dir1/user/bob mydomain/files/path/dir1 mydomain/files/user/bob在控制器中,我希望 params [:path] 包含 / path 与 /用户和 params [:user] 包含 / user (假设只有一个用户,它是可选的。)
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).
我正在寻找最好的方法,最好在 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'更多推荐
具有多个,可选和漂亮参数的路由
发布评论