简体   繁体   中英

Rails routes adding resource_id to url

Hi i have a rails app and i use different namespaces in it like user, admin etc.

My url's under user namespaces as

'/user/:controller_name' but i want to 

use it like

'/library/:library_id/user/:controller_name'

Users has one-to-one relationships with libraries. And i can get current users library id.

when i tried to make it with path parameter in route like

namespace :user, path: "library/user" do 
  ...
end

it is working but i couldnt get id.

Is it possible to do this?

This does not work?

namespace :user, path: "library/:library_id/user" do 
  ...
end

I guess, this is wat you are looking for

  resources :libraries do
    namespace :users do
      resources :resource_name
    end
  end


# Output for me
/libraries/:library_id/users/controller_name/new(.:format)
/libraries/:library_id/users/controller_name/:id/edit(.:format)
. 
.

This should work for you:

resources :libraries do
  member do
    resources :user do
      collection do
        resources :controller_name
      end
    end 
  end 
end

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