我遇到以下问题:当我转到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"
subdomain.rb
class Subdomain
def self.matches?(request)
request.subdomain.present? && request.subdomain != "www"
end
end
答案 0 :(得分:0)
删除!
上的爆炸find_by_subdomain
并提供默认子域
e.g:
@current_store = Store.find_by_subdomain(request.subdomains.first) || default_subdomain