2012-03-01 71 views
0

我有一個子域的應用程序。當用戶登錄到根域時。我想將他/她重定向到他/她的公司子域。我正在使用設計。redirect_to與軌道參數

這是我迄今爲止

def create 
    subdomain = request.subdomain 
    user = User.find_by_email(params[:user][:email]) 
    if subdomain.present? 
    resource = warden.authenticate!(auth_options) 
    set_flash_message(:notice, :signed_in) if is_navigational_format? 
    sign_in(resource_name, resource) 
    redirect_to statistics_url 
    else 
    redirect_to sign_in_at_subdomain_url(:subdomain => user.firm.subdomain), :params => params 
    end 
end 

def sign_in_at_subdomain 
    resource = warden.authenticate!(auth_options) 
    set_flash_message(:notice, :signed_in) if is_navigational_format? 
    sign_in(resource_name, resource) 
    redirect_to statistics_url 
end 

當用戶登錄低谷登錄表單,則params被髮送到創建動作,即設計用於驗證身份。正如你在我的代碼中看到的那樣,當一個子域出現時,登錄工作得很好,但是當它不存在時,我想要重定向到子域和sign_in_at_subdomain。

如何將parmas表單通過創建操作傳遞給sign_in_at_subdomain操作?

回答

1

您可以使用after_sign_in_path_for從根romain登錄用戶並將其簽入他所屬的子域。以下是來自example Rails 3 app with basecamp like subdomains and authentication(使用Devise)。

def after_sign_in_path_for(resource_or_scope) 
    scope = Devise::Mapping.find_scope!(resource_or_scope) 
    subdomain_name = current_user.subdomain.name 
    if current_subdomain.nil? 
    # logout of root domain and login by token to subdomain 
    token = Devise.friendly_token 
    current_user.loginable_token = token 
    current_user.save 
    sign_out(current_user) 
    flash[:notice] = nil 
    home_path = valid_user_url(token, :subdomain => subdomain_name) 
    return home_path 
    else 
    if subdomain_name != current_subdomain.name 
     # user not part of current_subdomain 
     sign_out(current_user) 
     flash[:notice] = nil 
     flash[:alert] = "Sorry, invalid user or password for subdomain" 
    end 
    end 
    super 
end 
+0

我用這種方法,但我不喜歡它。它將令牌存儲在user.loginable_token下的數據庫中。當它進入valid_user_url時,它從數據庫中提取用戶並將其登錄到當前子域中。這似乎很複雜。我只是想通過參數表單登錄表單。 – 2012-03-01 10:30:49

+0

這是我結束的解決方案。這是最好的。 – 2012-10-15 18:24:30