Rails中的递归路由 [英] Recursive routes in Rails

查看:63
本文介绍了Rails中的递归路由的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以在Rails中创建递归路由?

Is is possible to create a recursive route in Rails?

我有一个应用程序,允许管理员创建页面。页面模型是一个嵌套集,因此每个页面都有一个parent_id,因此页面是用树结构的。页面模型还使用Friendly ID插件为每个页面提供了信息。

I have an application, which allows a admin to create pages. The page model is a nested set and so each page has a parent_id hence the pages are structured in trees. The page model also uses the Friendly ID plugin to provide slugs for each page.

当用户浏览站点时,我希望他们看到URL中的嵌套结构-

When a user browses the site I would like them to see the nesting structure in the urls - its better for search engine purposes as well as any users who might like to browse the site by chopping the urls.

我想要类似以下内容的东西:

I want something along the lines of:

http://example.com/page/page/ page / page ... etc

现在显然我可以创建一个包含10个嵌套的嵌套地图,希望没有站点超过该限制,但是我m好奇是否还有其他方法...

Now obviously I can create a nested map with say 10 nests and hope that no site exceeds that limit, but I'm curious if there is another way...

推荐答案

您可以将初始路由(/ page)映射到控制器,设置为所有尾随参数设置 globbing

You can map the initial route (/page) to the controller, setting up "globbing" for all the trailing parameters.

map.connect '/:page/*pages', :controller => 'pages', :action => 'show' 

params [:pages]现在将包含一个页面参数数组(匹配许多您在网址中指定的结尾参数)。

params[:pages] will now contain an array of the page parameters (matching as many trailing params as you specify in the URL).

这篇关于Rails中的递归路由的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆