具有多个,可选和漂亮参数的路由

编程入门 行业动态 更新时间:2024-10-12 14:25:17
本文介绍了具有多个,可选和漂亮参数的路由的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我遇到了需要构造漂亮的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'

更多推荐

具有多个,可选和漂亮参数的路由

本文发布于:2023-11-25 23:18:07,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1631743.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:多个   可选   路由   参数   漂亮

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!