Github喜欢Rails中的路由

时间:2011-08-23 13:14:24

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

在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被视为有效网址 即使“D”不在列出的数组中,模块也设置为D. 上面

  2. 我想要网址localhost:3000/document_managers/A 如果额外参数不是,也会重定向到相同的动作,即new_tree 提供的URL中包含额外的参数
    localhost:3000/document_managers/C/121/1然后重定向网址 适当地到期望的控制器和动作,但如果只是URL 包含路径,直到模块名称Rails返回routes ActionController::UnknownAction我不知道为什么我已经 定义了控制器和动作。

1 个答案:

答案 0 :(得分:1)

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

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