One controller for multiple routes

半城伤御伤魂 提交于 2019-12-23 12:18:08

问题


I've been searching for a while now, but I can't seem to figure out if this is even possible. What I need is one controller for two different paths.

What I have is one model, with two types: own and compatitive.

So what I want is two paths like this, going both to one controller:

example.com/hotels

example.com/compatitives

These have to be resources, and there is going to be a lot of nesting in these routes. So I don't want to create a resource mapping for both of them.

I've already tried this:

resources :hotels, :compatitives, :controller => :hotels do

  resources :rooms do
    collection do
      match "/search", :action => :search
    end
  end

  collection do
    match "/search"
    match "/results/:type/:id(/:page)", :action => :results
  end

end

resources :prices do
  collection do
    match "/check"
  end
end

But the controller is not hotels_controller for both.

Is this even possible?

Thanks!


回答1:


Got it to work with this solution:

def add_hotel_collection
  resources :rooms do
    collection do
      match "/search", :action => :search
    end
  end
  collection do
    match "/search", :action => :search
    match "/results/:type/:id(/:page)", :action => :results
  end
end

resources :hotels do
  add_hotel_collection
end

resources :compatitives, :controller => :hotels do
  add_hotel_collection
end


来源:https://stackoverflow.com/questions/6872057/one-controller-for-multiple-routes

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