Rails:将子域路由到资源 [英] Rails: Routing subdomain to a resource
问题描述
是否可以将子域映射到资源?我有一个公司模型。目前,使用subdomain_fu,我的路由文件包含:
Is it possible to map a subdomain to a resource? I have a company model. Currently, using subdomain_fu, my routing file contains:
map.company_root '', :controller => 'companies', :action => 'show',
:conditions => { :subdomain => /.+/ }
我的公司模型包含子域列。
My Company model contains a "subdomain" column.
尽管这可以按预期工作,但它是一条命名的路线,并不会带来麻烦。本质上,我需要将 name.domain.com映射到公司控制器的show操作。是命名路由吗?还是我可以使用资源路由?
Whilst this works as intended, it's a named route and isn't restful. Essentially, I need to map "name.domain.com" to the show action for the companies controller. Is a named route the way to go, or can I use a resource route?
推荐答案
我不知道一种方法使用 map.resources
完成此操作。它确实接受:conditions
选项,但是我不确定如何删除URL的 / companies /
部分。但是, map.resources
主要是一种方便的方式来生成一堆命名路由,您可以手动执行。
I don't know of a way to do this with map.resources
. It does accept a :conditions
option but I'm not sure how to remove the /companies/
portion of the URL. However, map.resources
is primarily a convenient way to generate a bunch of named routes, which you can do manually. Something like this.
map.company '', :controller => 'companies', :action => 'show', :conditions => { :subdomain => /.+/, :method => :get }
map.new_company 'new', :controller => 'companies', :action => 'new', :conditions => { :subdomain => /.+/, :method => :get }
map.edit_company 'edit', :controller => 'companies', :action => 'edit', :conditions => { :subdomain => /.+/, :method => :get }
map.connect '', :controller => 'companies', :action => 'create', :conditions => { :subdomain => /.+/, :method => :post }
map.connect '', :controller => 'companies', :action => 'update', :conditions => { :subdomain => /.+/, :method => :put }
map.connect '', :controller => 'companies', :action => 'destroy', :conditions => { :subdomain => /.+/, :method => :delete }
未经测试,但可以让您接近。
Untested, but it should get you close.
这篇关于Rails:将子域路由到资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!