building a devise rails app with user accounts as subdomains Im unable to figure out how to redirect to a default ( default.domain.com) subdomain when the subdomain a user visits is non existant.
For example:
- user.domain.com works ( user exists in database )
- user2.domain.com fails ( user not in database ) and should be redirected to default.domain.com
How can this be accomplished?
Im using below code but the redirect based on Rails.env gets in a never ending loop 🙁
class ApplicationController < ActionController::Base
protect_from_forgery
layout "application"
before_filter :account
def account
@user = User.where(:subdomain => request.subdomain).first || not_found
end
def not_found
# next 2 lines is a temp solution--- >
raise ActionController::RoutingError.new('User Not Found')
return
# --- > this below fails results in endless loop
if Rails.env == "development"
redirect_to "http://default.domain.dev:3000"
return
else
redirect_to "http://default.domain.com"
end
end
end
Not sure there is going to be a particularly nice way to do this and it’s not easy to make a proper judgement here without seeing the bigger picture, but, maybe you should store the default domain as a constant somewhere and then check this before redirecting, to break the loop, as it were!
Something like this would be better;
You get the general idea, you could set DEFAULT_URL in an initializer..