redirect_to with rails中的params

时间:2012-03-01 09:25:32

标签: ruby-on-rails redirect devise

我有一个带子域名的应用程序。用户在根域登录时。我想将他/她重定向到他/她的公司子域。我正在使用设计。

这是我到目前为止所拥有的

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会被发送到create action,用于身份验证。正如您在我的代码中看到的那样,当存在子域时,登录效果很好,但是当它不存在时,我想重定向到子域和sign_in_at_subdomain。

如何通过创建操作将表格中的parmas传递给sign_in_at_subdomain操作?

1 个答案:

答案 0 :(得分:1)

您可以使用after_sign_in_path_for从root 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