在我的 gem 文件中,我添加了 gem 'subdomain-fu', '1.0.0.beta2'
。然后我做到了bundle install
。我试图打印 <%= current_subdomain %> 但我得到了undefined local variable or method
current_subdomain'` 。为什么我会收到此错误?
我也重新启动了我的 nginx 服务器和独角兽。
在我的 gem 文件中,我添加了 gem 'subdomain-fu', '1.0.0.beta2'
。然后我做到了bundle install
。我试图打印 <%= current_subdomain %> 但我得到了undefined local variable or method
current_subdomain'` 。为什么我会收到此错误?
我也重新启动了我的 nginx 服务器和独角兽。
在 Rails 中添加 sudomain 不需要 gem。
这是我成功添加到我的一个应用程序中的一些代码。
在您routes.rb
添加以下内容:
constraints subdomain: false do
root to: 'landings#index'
end
constraints subdomain: 'my' do
get '/', to: 'users#show', as: 'app_root'
resources :users
resources :games do
collection do
get :search, to: 'games#search', as: :search
end
end
end
以下给了我:
root GET / landings#index
app_root GET / users#show {:subdomain=>"my"}
users GET /users(.:format) users#index {:subdomain=>"my"}
POST /users(.:format) users#create {:subdomain=>"my"}
new_user GET /users/new(.:format) users#new {:subdomain=>"my"}
edit_user GET /users/:id/edit(.:format) users#edit {:subdomain=>"my"}
user GET /users/:id(.:format) users#show {:subdomain=>"my"}
PATCH /users/:id(.:format) users#update {:subdomain=>"my"}
PUT /users/:id(.:format) users#update {:subdomain=>"my"}
DELETE /users/:id(.:format) users#destroy {:subdomain=>"my"}
search_games GET /games/search(.:format) games#search {:subdomain=>"my"}
games GET /games(.:format) games#index {:subdomain=>"my"}
POST /games(.:format) games#create {:subdomain=>"my"}
new_game GET /games/new(.:format) games#new {:subdomain=>"my"}
edit_game GET /games/:id/edit(.:format) games#edit {:subdomain=>"my"}
game GET /games/:id(.:format) games#show {:subdomain=>"my"}
PATCH /games/:id(.:format) games#update {:subdomain=>"my"}
PUT /games/:id(.:format) games#update {:subdomain=>"my"}
DELETE /games/:id(.:format) games#destroy {:subdomain=>"my"}
这种方法的问题在于,对于子域路由,您必须使用search_games_path(subdomain: 'mysubdomainname')
或search_games_url (which automatically points to your subdomain)
Rails 4 带有内置功能,因此您不需要使用旧的 gem,例如 subdomain_fu。
要获取 rails 4 中的当前子域,只需执行以下操作:
<%= request.subdomain %>