1

In my gem file i have added gem 'subdomain-fu', '1.0.0.beta2' . Then I did bundle install. I tried to print <%= current_subdomain %> but I got undefined local variable or methodcurrent_subdomain'` . Why am I getting this error?

I have also restarted my nginx server and unicorn too.

Raaz
  • 1,669
  • 2
  • 24
  • 48

2 Answers2

2

You don't need a gem to add sudomains in Rails.

Here's some code I've sucesffully added into one of my apps.

In your routes.rb add the following:

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

The following gives me:

                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"}

The catch with this method is that for subdomains routes, you have to use search_games_path(subdomain: 'mysubdomainname') or search_games_url (which automatically points to your subdomain)

Vlad
  • 902
  • 4
  • 14
0

Rails 4 comes with functionality built in so you dont need to use old gems such as subdomain_fu.

To get the current subdomain in rails 4, simply do:

<%= request.subdomain %>
Scott
  • 1,105
  • 1
  • 7
  • 17