Reputation: 229
my rails 3 apps need a before_filter for check if user are logged or not. if logged, redirect to subdomain. I use Devise for log system. After sign_up the user are redirect to his subdomain, but if he come back to the website and are logged again, he must be redirected to his subdomain again.
I tried this :
if current_user.present?
company_id = current_user.company_id
@company = Company.find(company_id)
redirect_to @company.subdomain + "." + request.domain
end
with this code, the url are : www.mywebsite.comsubdomain.mywebsite.com Yeah, i think it's because redirect_to are for webpage not for new url.
I tried with
if current_user.present?
company_id = current_user.company_id
@company = Company.find(company_id)
root_url(:host => @company.subdomain + "." + request.domain)
end
but same thing. i get the same url as previous tests.
what can i do to redirect to my subdomain?
Upvotes: 3
Views: 10709
Reputation: 16011
This answer in another stackoverflow question should be your answer too: https://stackoverflow.com/a/327154/346693
What that answer provided:
class ApplicationController < ActionController::Base
before_filter :check_uri
def check_uri
redirect_to request.protocol + "www." + request.host_with_port + request.request_uri if !/^www/.match(request.host)
end
end
The problem of your way is you are not providing a full url (http://sth.com vs sth.com)
Upvotes: 2
Reputation:
Consider this example: another question.
You can use this as a starting place. Basically you would wrap the call to authenticate_user! inside your own before handler and if authenticate_user! raises an exception you can do your redirect.
If it doesn't raise the exception you can redirect somewhere else.
Upvotes: 0