是否可以使用acts_as_tree 来实现类似文件系统的路由?

发布于 2024-08-10 09:31:33 字数 352 浏览 14 评论 0原文

我有一个 Folder 模型,其中 acts_as_tree

路由是否有可能代表文件夹结构?

文件夹:

1: Folder A
  2: Folder B
  3: Folder C
    4: Folder D
5: Folder E

路线:

/folders/1
/folders/1/2
/folders/1/3/4
/folders/1/2/new
/folders/...

这可能吗?

I have a Folder model which acts_as_tree.

Is it possible that the routes represent the folder structure?

Folders:

1: Folder A
  2: Folder B
  3: Folder C
    4: Folder D
5: Folder E

Routes:

/folders/1
/folders/1/2
/folders/1/3/4
/folders/1/2/new
/folders/...

Is this possible?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

北方的韩爷 2024-08-17 09:31:33

据我所知,没有简单的方法可以做到这一点。

我认为最好的方法是使用一组命名路由。

我还没有测试过这个,所以不能保证,但即使失败它也应该给你一些提示。

map.my_new_folder '/folders/:ancestors/:id/new', :controller => "folders",
 :action => "new", :ancestors => /^(\d+\/?)*$/
map.my_show_folder, '/folders/:ancestors/:id', :controller => "folders",
 :action => "show", :ancestors => /^(\d+\/?)*$/
map.my_edit_folder '/folders/:ancestors/:id/edit', :controller => "folders",
 :action => "edit", :ancestors => /^(\d+\/?)*$/
...

您必须自己形成祖先字符串并将其作为选项传递给 my_x_folder_path。但你应该得到你想要的漂亮的网址。您可能最好将其提取到一个帮助程序中,该帮助程序还可以根据祖先的存在动态选择资源路径或命名路由路径。

So far as I can tell, there's no easy way to do this.

I think the best way to do this is with a set of named routes.

I haven't tested this, so there's no guarantees, but it should give you some hints even if it fails.

map.my_new_folder '/folders/:ancestors/:id/new', :controller => "folders",
 :action => "new", :ancestors => /^(\d+\/?)*$/
map.my_show_folder, '/folders/:ancestors/:id', :controller => "folders",
 :action => "show", :ancestors => /^(\d+\/?)*$/
map.my_edit_folder '/folders/:ancestors/:id/edit', :controller => "folders",
 :action => "edit", :ancestors => /^(\d+\/?)*$/
...

You'll have to form the ancestor string yourself and pass it as an option to my_x_folder_path. But you should get the pretty urls you want. You might be better of extracting it to a helper that can also dynamically select the resource path or the named route path based on the existence of ancestors.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文