2015-10-15 37 views
0

在我的寶石文件中,我添加了寶石'subdomain-fu', '1.0.0.beta2'。然後我做了bundle install。我試圖打印<%= current_subdomain%>但我得到了undefined local variable or method current_subdomain'`。爲什麼我得到這個錯誤?subdomain-fu不能在導軌中工作4

我也重新啓動了我的nginx服務器和獨角獸。

回答

1

你不需要一個gem在Rails中添加sudomains。

下面是一些我已經完成添加到我的應用程序中的代碼。

在你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)

0

Rails 4自帶內置的功能,因此您不需要使用諸如subdomain_fu之類的舊寶石。

要獲得軌道4當前子域,簡單地做:

<%= request.subdomain %>