2011-07-12 113 views
1

从Rails 2.3.x + gem subdomain_routes移植时,我遇到了有关Rails 3中子域的路由问题。随着subdomain_routes宝石,我能够通过模型等方法容易映射的路线如下:Rails 3子域路由和URL助手

# config/routes.rb 
map.subdomain :model => :site do |site| 
    resources :pages 
end 

这将产生网址帮手,像site_pages_url并且可以像这样使用:

# console 
@site = Site.find_by_subdomain(“yehuda”) 
app.site_pages_url(@site) => http://yehuda.example.com/pages 
app.site_page_url(@site, @page) => http://yehuda.example.com/page/routes-rock 

在Rails 3本粗略地将转化为:

# config/routes.rb 
class SiteSubdomain 
    def self.matches?(request) 
    request.subdomain.present? && request.subdomain != 'www' && 
     request.params[:site_id].present? 
    end 
end 

Blog::Application.routes.draw do 
    resources :sites do 
    constraints(SiteSubdomain) do 
     resources :pages 
    end 
    end 
end 

和重载标准的url_for应的基本工作原理就像subdomain_routes:

module UrlFor 

    def with_subdomain(subdomain) 
    subdomain = (subdomain || "") 
    subdomain += "." unless subdomain.empty? 
    [subdomain, request.domain, request.port_string].join 
    end 

    def url_for(options = nil) 
    if options.kind_of?(Hash) && options.has_key?(:subdomain) 
     options[:host] = with_subdomain(options.delete(:subdomain)) 
    end 
    super 
    end 

end 
ActionDispatch::Routing::UrlFor.send(:include, UrlFor) 

然而,网址助手仍然没有产生,而不是预期http://yehuda.example.com/pages

+0

你对这个问题有什么消息吗? –

+0

@chinshr任何新闻或更新? –

回答

0

您已命名的班级为

SiteSubdomain 

这实际上意味着,你的路线正确的URL像site_pages_url(@site) #=> http://www.example.com/pages,应该有SiteSubdomain而不是Subdomain。修改为

Blog::Application.routes.draw do 
    resources :sites do 
    constraints(SiteSubdomain) do 
     resources :kases 
    end 
    end 
end 
+0

我刚纠正了我的问题中的错误。问题是,我似乎仍然无法产生正确的路线,例如'site_pages_url(@site)#=> http:// yehuda.example.com/pages' – chinshr