简体   繁体   中英

Rails 2.3 routing namespace resources with custom variables

Rails 2.3.11

So I have a namespace where I want to specify the 'path' for each item underneath it for example:

accounts.namespace(:accounts) do |f|
  f.resources :sub_accounts, :path => "sub_accounts/:account_number/:sub_account"
end

would generate:

GET     /accounts/sub_accounts/:account_number/:sub_account index   
GET     /accounts/sub_accounts/:account_number/:sub_account new 
POST    /accounts/sub_accounts/:account_number/:sub_account create  
GET     /accounts/sub_accounts/:account_number/:sub_account show    
GET     /accounts/sub_accounts/:account_number/:sub_account edit    
PUT     /accounts/sub_accounts/:account_number/:sub_account update  
DELETE  /accounts/sub_accounts/:account_number/:sub_account destroy 

I know you can do this with something like:

map.connect '/accounts/sub_accounts/:account_number/:sub_account', :controller => "accounts/sub_accounts", :action => "index"

GET     /accounts/sub_accounts/:account_number/:sub_account index   

However the variables will be consistent across all routes in the accounts namespace and I didn't want to have to type all that out each time. Is there a way to do this?

You can use the path_prefix option. This will prepend that path to your resources.

f.resources :sub_accounts, :path_prefix => 'sub_accounts'

Read more at the documentation for resources: http://railsapi.com/doc/rails-v2.3.8/classes/ActionController/Resources.html#M002114

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM