2011-07-12 17 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のようなURLヘルパーを生成すると、次のように使用することができます:Railsの3で

# 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 

この大まかに変換します:

# 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) 

ただし、URLヘルパーはまだ代わりにあなたが効果的に意味

SiteSubdomain 

としてクラス、あなたのルート名付けまし予想http://yehuda.example.com/pages

+0

あなたはこの質問に関するニュースをお持ちですか? –

+0

@chinshrこれに関するニュースやアップデートはありますか? –

答えて

0

の、site_pages_url(@site) #=> http://www.example.com/pagesなどの正しいURLを生成しません。サブドメインの代わりにSiteSubdomainが必要です。変更する

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

関連する問題