我尝试在成功注册到各自的子域后重定向用户。我在application_controller中输入了以下代码。
application_controller.rb
protected
def after_sign_in_path_for(resource)
redirect_to root_url(subdomain: @users.subdomain)
end
我收到以下错误
NoMethodError in Devise::SessionsController#create
undefined method `subdomain' for nil:NilClass
Extracted source (around line #10):
def after_sign_up_path_for(subdomain)
redirect_to root_url(subdomain: @users.subdomain)
end
答案 0 :(得分:0)
Devise中的NoMethodError :: SessionsController #create undefined方法 `子域'为零:NilClass
@users
是nil
,因此Rails会发出错误。当您使用Devise时,可以使用User
访问 Devise模型(resource
)实例。
def after_sign_in_path_for(resource)
redirect_to root_url(subdomain: resource.subdomain)
end
您也可以使用current_user
def after_sign_in_path_for(resource)
redirect_to root_url(subdomain: current_user.subdomain)
end