Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

One controller for multiple routes

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!

like image 675
Tim Baas Avatar asked Nov 14 '22 19:11

Tim Baas


1 Answers

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
like image 84
Tim Baas Avatar answered Jan 07 '23 03:01

Tim Baas