一台控制器可处理多条路线 [英] One controller for multiple routes

查看:67
本文介绍了一台控制器可处理多条路线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经搜索了一段时间,但我似乎无法弄清楚是否有可能。
我需要的是一个用于两条不同路径的控制器。

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.

我已经尝试过:

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

但控制器不是这两者的hotels_controller。

But the controller is not hotels_controller for both.

这甚至可能吗?

谢谢!

推荐答案

让它与该​​解决方案一起使用:

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

这篇关于一台控制器可处理多条路线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆