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
あなたはこの質問に関するニュースをお持ちですか? –
@chinshrこれに関するニュースやアップデートはありますか? –