从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
你对这个问题有什么消息吗? –
@chinshr任何新闻或更新? –