【问题标题】:Github like routes in Rails在 Rails 中类似 Github 的路线
【发布时间】:2011-08-23 13:14:24
【问题描述】:

在 Rails 中使用类似 github 的链式路由

我有类似这样的网址:

'localhost:3000/document_managers/[:module_name]'
'localhost:3000/document_managers/[:module_name]/1/2/3/.'  # can be any level deep

这是他们的路线定义:

map.connect '/document_managers/:module',
  :controller => "document_managers",
  :action => :new_tree,
  :module => ["A","B","C"]

map.connect '/docuemnt_managers/:module/*path',
  :controller => "document_managers",
  :action => "new_tree",
  :module => ["A","B","C"]

问题来了:

  1. 模块名称值不能是任何东西,除了来自 上面给出的数组,即(“A”,“B”,“C”),在任何时候,URL 都必须类似于

    localhost:3000/document_managers/A/1

    localhost:3000/document_managers/B/221/1

    localhost:3000/document_managers/C/121/1

    但事实并非如此 localhost:3000/document_managers/D/121/1 被视为有效 url 并且模块设置为 D,即使“D”不在列出的数组中 以上

  2. 我希望 URL localhost:3000/document_managers/A 如果额外参数不是,也重定向到相同的操作,即 new_tree 在 URL 中提供包含额外参数
    localhost:3000/document_managers/C/121/1 然后 URL 被重定向 适合所需的控制器和操作,但如果仅 URL 包含路径,直到 Rails 的模块名称返回 routes ActionController::UnknownAction 我不知道为什么,因为我已经 定义了控制器和动作。

【问题讨论】:

  • 答案可能是不可接受的,但评论给出的答案将有助于其他用户理解他的问题并相应地调整他们的答案。
  • 对不起,我不知道这样的事情我会确保我会接受所有的答案,但看看我的问题列表,我还没有找到人回答我的问题,所以接受什么

标签: ruby-on-rails ruby url-routing


【解决方案1】:

在 Rails 3.1 中,您可以在路由文件中执行此操作以获得所需的内容:

match '/document_managers/:module', 
  :controller => "document_managers",
  :action => :new_tree, 
  :constraints => {:module => /[ABC]/}

【讨论】:

  • 你有吗??我怎么错过了约束条件谢谢@RyanTM
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-01-22
  • 2015-03-04
  • 2011-06-13
  • 1970-01-01
相关资源
最近更新 更多