简体   繁体   中英

How to make two rails routes go to the same controller when namespaced?

The intention is to create the following two urls:

Where "posts" are resources, and "widgets" are not (it's just a URL the client wants)

I want both of these routes to go to the SAME controller.

I have a setup like this in routes.rb:

 namespace :widgets do
   get :edit
 end

 resources :posts do
   namespace :widgets do
     get :edit, to: "widgets#edit"
   end
 end

a quick rake routes/grep shows these go to two seperate controllers:

 rake routes | grep widgets
 /widgets/edit(.:format)                widgets#edit
 /posts/:post_id/widgets/edit(.:format) widgets/widgets#edit

I have both of the urls I want, but the second one is going to a different controller! I've tried a whole bunch of different configurations with no luck - how do I bust out of that additional "widgets" namespace in the second route without letting go of this URL that the client wants?

I am using rails 3.2, so "concerns" are out. Upgrading not an option right now.

Thanks in advance.

I would separate that route from being nested above your resource route block.

get "posts/:post_id/widgets/edit" => 'widgets#edit'.

Your nested namespace is adding the widget slug and your desired action is adding the model slug as well.

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