Tim Baas
Tim Baas

Reputation: 6185

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!

Upvotes: 5

Views: 3309

Answers (1)

Tim Baas
Tim Baas

Reputation: 6185

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

Upvotes: 1

Related Questions