How to remove controller names from rails routes?

ⅰ亾dé卋堺 提交于 2019-11-28 16:48:01

问题


I would like to trim down the routes on my application so that:

http://myapplication.com/users/peter/questions/how-do-i-create-urls

becomes...

http://myapplication.com/peter/how-do-i-create-urls

I have a users controller and would like it to be resourceful. Users also have a nested resource called questions.

Basic routes file

Without any URL trimming, the routes file looks like this:

...
resources :users do
  resources :questions
end

However the URLs from this take the form of

http://myapplication.com/users/peter/questions/how-do-i-create-urls

rather than

http://myapplication.com/peter/how-do-i-create-urls

Partial success I have tried doing the following:

...
resources :users, :path => '' do
  resources :questions
end

This works and produces:

http://myapplication.com/peter/questions/how-do-i-create-urls

However if I try:

...
resources :users, :path => '' do
  resources :questions, :path => ''
end

Then things start to go wrong.

Is this the right approach and if so, can it be made to work with nested resources too?


回答1:


The way you are doing it should work. I don't know what problem you are experiencing but if you copied the example code from your app directly then it might be because of the extra end that you have put in your routes. It should probably look like this:

resource :users, :path => '' do
  resource :questions, :path => ''
end

Another thing that could be the cause and that you need to be vary careful about is that these routes pretty much catches all requests and you should have them last in your routes.rb so that other routes matches first. Take this scenario for example:

resource :users, :path => '' do
  resource :questions, :path => ''
end

resources :posts

If you do it this way then no request will ever be routed to the Posts controller since a request to /posts/1 will be sent to the Questions controller with :user_id => 'posts', :id => 1

Edit:

Also, I now noticed that you use resource instead of resources. Don't know if that is intended or if it is a mistake.




回答2:


Thanks to both @mark and @DanneManne for their help. With their input and a little more tweaking I got it all working. It's not exactly trivial but I'm not sure you could make it much shorter either:


Final working code

# setup the basic resources while holding some back for creation below
resources :users, :except => [:show, :index, :new, :create], :path => '/' do
  resources :questions, :except => [:show]
end

# for clarity, pick out routes that would otherwise go 
# to root (such as new_user => '/new')
resources :users, :only => [:index, :new, :create]


# setup questions#show to give clean URLS
match ':user_id/:question_id', :as => :user_question, 
                               :via => :get,
                               :controller => :questions, 
                               :action => :show

# setup users#show method to give clean URLS
match ':user_id', :as => :user, 
                  :via => :get, 
                  :controller => :user, 
                  :action => :show

Rake Routes output

    user_questions GET    /:user_id/questions(.:format)          {:action=>"index", :controller=>"questions"}
                   POST   /:user_id/questions(.:format)          {:action=>"create", :controller=>"questions"}
 new_user_question GET    /:user_id/questions/new(.:format)      {:action=>"new", :controller=>"questions"}
edit_user_question GET    /:user_id/questions/:id/edit(.:format) {:action=>"edit", :controller=>"questions"}
     user_question PUT    /:user_id/questions/:id(.:format)      {:action=>"update", :controller=>"questions"}
                   DELETE /:user_id/questions/:id(.:format)      {:action=>"destroy", :controller=>"questions"}
         edit_user GET    /:id/edit(.:format)                    {:action=>"edit", :controller=>"users"}
              user PUT    /:id(.:format)                         {:action=>"update", :controller=>"users"}
                   DELETE /:id(.:format)                         {:action=>"destroy", :controller=>"users"}
             users GET    /users(.:format)                       {:action=>"index", :controller=>"users"}
                   POST   /users(.:format)                       {:action=>"create", :controller=>"users"}
          new_user GET    /users/new(.:format)                   {:action=>"new", :controller=>"users"}
     user_question GET    /:user_id/:question_id(.:format)       {:controller=>"questions", :action=>"show"}
              user GET    /:user_id(.:format)                    {:controller=>"user", :action=>"show"}



回答3:


Not sure about the nesting but try

:path => '/'



回答4:


Just thought I'd add another possible solution, in case anybody else arrives from Google at a later date.

After looking at this nested resource article, this could be a cleaner solution with almost the same flexibility in routes:

resources :users, :path => ''
resources :users, :path => '', :only => [] do
  resources :questions, :path => '', :except => [:index]

Basically, by including the second parent block, the child resources aren't yielded before the parent resources.

This particular example also assumes that having complete routes for the parent block is more crucial than those for the child. Consequently, the child block is limited from having an index, but this might be acceptable, depending on the situation.



来源:https://stackoverflow.com/questions/6369476/how-to-remove-controller-names-from-rails-routes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!