Subdomains in a Rails 4 app

时光怂恿深爱的人放手 提交于 2019-11-30 07:45:44
Richard Peck

There are several issues you may have

The first is you need to appreciate the way in which Devise redirects your user after login, and secondly how subdomains are routed in Rails.

--

Devise

By default, Devise routes to current_user_path (which typically means users#show) or something in your routes:

def after_sign_in_path_for(resource)
  current_user_path
end

This means when you accept the login from the user, they will be taken to their own path. Depending on your routes, this will generally mean the main site (no subdomain) user's path (domain.com/users/56) or something.

Without any specifics on this from your question, I can only speculate on this.

--

Subdomains

Having just worked on some subdomain-enabled apps, there is something you should consider about routing to subdomains.

Once your user has signed in, they need to be able to be routed to a specific subdomain. The way to do this is to use a constraint in your routes:

#config/routes.rb
constraints { subdomain: 'admin' } do
    resources :photos
end

We've found you cannot do this using normal routing paths - you have to use the url (not path helper). For example:

photos_path(subdomain: current_user.name) #-> does not work (path is relative)
photos_url(subdomain: current_user.name) #-> will route to http://name.lvh.me:3000

What you have to remember is if you're looking to redirect / route traffic to different subdomains, you will need to reference the url form of the helper, not the path reference.

So if you take the after_sign_in_path_for as shown above, you'll want to do something like this:

def after_sign_in_path_for(resource)
   root_url(subdomain: resource.name)
end

--

Sessions

Finally, you want to ensure your Devise session cookies will remain initialized after you've set them. We've found subdomains aren't handled by default, so you have to ensure they are catered for:

Share session (cookies) between subdomains in Rails?

#config/initializers/session_store.rb
YOUR_APP_NAME::Application.config.session_store :cookie_store, key: '_app_name_session', domain: :all, tld_length: 2 

I solved it manually. I found out the 'root' directive in the routes.rb accepts requests from every subdomain. In the end, I implemented a custom constraint and added this to the constraint for the devise_for in the routes.rb file. This adequately solved my problem. Thank you all for your help!

Update:

In response to the comment asking for my code: we had a system set up whereby each account (business account) would have their own subdomain, and each business account would have many users. My subdomain constraint looks like

lib/subdomain.rb

class Subdomain
  def self.matches?(request)
    request.subdomain.present? && Account.exists?(subdomain: request.subdomain)
  end
end

Then in my config/routes.rb I used that constrain to route the requests:

config/routes.rb

require 'Subdomain'

Rails.application.routes.draw do
  # Some routes...
  constraints(Subdomain) do
    devise_for :users, controllers: {:sessions => 'session'}
    root 'dashboard#index'
    # More subdomain constrained routes...
  end
end

In my original question I'd posted I'd already shown the rest of my setup. I hope this helps someone.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!