Rails 设计 - 注册时使用不同的重定向路径
Rails Devise - Different redirect paths on signup
我正在为用户使用设计,但我有两种类型的用户(客户和供应商),并且需要基于他们遵循的路径的不同重定向路由。例如:如果客户注册 (/signup
),它将把他们重定向到他们的仪表板。如果供应商注册 (/suppliers/registrations/user
),则需要将他们引导到下一个表格,他们开始描述他们的业务 (/suppliers/registrations/business
)。你如何管理这个?
更新
我更新了我的设计注册控制器以包含以下内容(我排除了所有注释掉的内容)
users/registrations_controller.rb
class Users::RegistrationsController < Devise::RegistrationsController
protected
def after_sign_up_path_for(resource)
if resource.supplier == true
redirect_to supplier_business_path
elsif resource.supplier == false
redirect_to user_projects_path(current_user)
end
end
end
但不管怎样,它总是把我带到根源上。
在应用程序控制器中使用设计 after_sign_in_path_for
方法。例如
def after_sign_in_path_for(resource)
if resource.role == "customer"
redirect_to customer_dashboard_path
elsif resource.role == "supplier"
redirect_to supplier_dashboard_path
end
end
after_sign_in_path_for 应该只是 return 路径但不执行重定向,请尝试像这样更改您的方法:
protected
def after_sign_up_path_for(resource)
if resource.supplier == true
# also add specific url = '/suppliers/registrations/user'
supplier_business_path
elsif resource.supplier == false
user_projects_path(current_user)
end
end
我正在为用户使用设计,但我有两种类型的用户(客户和供应商),并且需要基于他们遵循的路径的不同重定向路由。例如:如果客户注册 (/signup
),它将把他们重定向到他们的仪表板。如果供应商注册 (/suppliers/registrations/user
),则需要将他们引导到下一个表格,他们开始描述他们的业务 (/suppliers/registrations/business
)。你如何管理这个?
更新 我更新了我的设计注册控制器以包含以下内容(我排除了所有注释掉的内容)
users/registrations_controller.rb
class Users::RegistrationsController < Devise::RegistrationsController
protected
def after_sign_up_path_for(resource)
if resource.supplier == true
redirect_to supplier_business_path
elsif resource.supplier == false
redirect_to user_projects_path(current_user)
end
end
end
但不管怎样,它总是把我带到根源上。
在应用程序控制器中使用设计 after_sign_in_path_for
方法。例如
def after_sign_in_path_for(resource)
if resource.role == "customer"
redirect_to customer_dashboard_path
elsif resource.role == "supplier"
redirect_to supplier_dashboard_path
end
end
after_sign_in_path_for 应该只是 return 路径但不执行重定向,请尝试像这样更改您的方法:
protected
def after_sign_up_path_for(resource)
if resource.supplier == true
# also add specific url = '/suppliers/registrations/user'
supplier_business_path
elsif resource.supplier == false
user_projects_path(current_user)
end
end