Github like routes in Rails

2019-06-06 10:15发布

Using github like chain routes in rails

I have URLs similar to this:

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

Here is the route definition for them:

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"]

Here is the problem:

  1. The idea that module name value can't be anything except from the given above array i.e("A","B","C") like at any time the URL must be something like

    localhost:3000/document_managers/A/1 or

    localhost:3000/document_managers/B/221/1 or

    localhost:3000/document_managers/C/121/1

    but that not the case even though localhost:3000/document_managers/D/121/1 is treated as valid url and module is set to D even though the "D" is not in listed array above

  2. I want the the URL localhost:3000/document_managers/A to also redirect to same action i.e new_tree if the extra parameter isn't provided as in the URL contain extra parameters
    localhost:3000/document_managers/C/121/1 then the URL is redirected appropriately to the desired controller and action but if the URL only contain the path until the module name the Rails return a routes ActionController::UnknownAction I don't know why as I have already defined the controller and action.

1条回答
Animai°情兽
2楼-- · 2019-06-06 10:34

In Rails 3.1, you can do this in your routes file to get what you want:

match '/document_managers/:module', 
  :controller => "document_managers",
  :action => :new_tree, 
  :constraints => {:module => /[ABC]/}
查看更多
登录 后发表回答