我遇到以下问题:当我转到http://lvh.me:3000或http://www.lvh.me:3000,我得到这个错误:
找不到子域=的存储
app/controllers/application_controller.rb:8:在"set_current_store"中
使用子域,效果很好(所以如果我转到http://store.lvh.me,它工作得很好。只有根不起作用)。
这是我的代码:
url_helper.rb
module UrlHelper
def with_subdomain(subdomain)
subdomain = (subdomain || "")
subdomain += "." unless subdomain.empty?
[subdomain, request.domain].join
end
end
application_controller.rb
class ApplicationController < ActionController::Base
before_filter :set_current_store
include UrlHelper
protect_from_forgery
private
def set_current_store
@current_store = Store.find_by_subdomain!(request.subdomains.first)
end
end
routes.rb
resources :stores
constraints(Subdomain) do
match '/' => "stores#show"
resources :stores
resources :pages, path: "strani"
end
root to: "stores#index"
子域.rb
class Subdomain
def self.matches?(request)
request.subdomain.present? && request.subdomain != "www"
end
end
删除find_by_subdomain
上的bang !
并提供默认子域
例如:
@current_store = Store.find_by_subdomain(request.subdomains.first) || default_subdomain