在rails中具有params的重定向_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。

如何通过创建操作将表单中的参数传递给sign_in_at_subdomain操作?

您可以使用after_sign_in_path_for从root romain注销用户,并将其登录到他所属的子域。以下是带有类似大本营的子域和身份验证(使用Devise)的示例Rails3应用程序。

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

最新更新