复杂的轨道路线
我想做一些像 github 那样的嵌套 url 的事情,就像 如何路由用户配置文件 URL 以跳过控制器? 但不太确定如何继续操作。
例如,查看他们的提交:':user/:repo/commit/:sha',控制器正在提交。我将如何复制这种类型的嵌套资源?
谢谢 :)
I would like to do something like github has with nested urls, and like How do I route user profile URLs to skip the controller? but not really sure how to go on with it.
For example, looking at a commit they have: ':user/:repo/commit/:sha', with the controller being commit. How would I replicate this type of nested resource?
thank you :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果 commit 是一个 RESTful 控制器,它使用 :sha 而不是 id 来查找记录。你可以这样做:
它将创建标准的 RESTful 路由,看起来像
http://yoursite.tld/:user/:repo/commit/:id
再说一次,如果你永远不会翻译url 中的 id 部分为提交 id,那么您没有理由不能将其用作 :sha 值。
示例:
您可能还想覆盖提交模型中的 to_param 以返回 sha 值。
这样,现在
link_to commit_url(@commit, :user => current_user, :repo => @repo)
将提供与您的方案匹配的 url。If commit is a RESTful controller that uses :sha instead of an id to find records. You could do this instead:
It will create the standard RESTful routes that look like
http://yoursite.tld/:user/:repo/commit/:id
Again, if you'll never be translating the id portion in the url to a commit id, then there's no reason you can't use it as a :sha value.
example:
You may also want to over ride to_param in the commit model to return the sha value.
So that now
link_to commit_url(@commit, :user => current_user, :repo => @repo)
will provide a url that matches your scheme.怎么样
如果您需要 RESTful 路由,或者使用
map.resource
而不是map.connect
。在控制器中,可以从 params 中检索 URL 信息,例如
params[:user]
返回用户名。How about
Or use
map.resource
instead ofmap.connect
if you need a RESTful route.In the controller, the URL information can be retrieved from params, for example
params[:user]
returns the username.您可以根据需要命名路线,并指定要使用它们的控制器和操作。
例如,您可能有:
这会将请求发送到“事务”控制器中的“提交”方法。
然后可以使用参数在控制器中访问其他变量:
You can name your routes as you like, and specify which controllers and actions you'd like to use them with.
For example, you might have:
This would send the request to the 'commit' method in 'transactions' controller.
The other variables can then be accessed in your controller using params: