gpt4 book ai didi

ruby-on-rails - 无法登录用户资源 - devise/sessionsController#create 中的 NoMethod 错误

转载 作者:数据小太阳 更新时间:2023-10-29 07:26:20 25 4
gpt4 key购买 nike

我在使用 Rails 3 和 Devise 时遇到了一个特殊问题。我的 Rails 应用程序有 3 种用户类型:
客户、所有者和管理员

routes.rb:

   devise_for :admins
devise_for :owners
devise_for :clients

application_controller.rb:

def after_sign_in_path_for(resource)
if resource == "client"
"/client_accounts/" + current_client.id.to_s
elsif resource == "admin"
stored_location_for(:admins) || "/admin/control_panel"
elsif resource == "owner"
stored_location_for(:owners) || "/client_accounts"
end
end

我的所有者和管理员登录工作正常,但我无法让客户登录工作。这是我得到的错误:

NoMethodError in Devise/sessionsController#create
undefined method `client_url' for #<Devise::SessionsController:0x10a98f868>

应用程序跟踪为空。

最佳答案

根据Devise docs after_sign_in_path_for 方法采用实际的 Client 对象,但您将输入与一组字符串进行比较,这些字符串都将返回 false,因此您的大 if 子句将返回 nil。不确定发生这种情况时 Devise 的设计目的是什么,但寻找“client_url”将是一个合理的默认值。

试试这个:

def after_sign_in_path_for(resource)
case resource
when Client then "/client_accounts/" + current_client.id.to_s
when Admin then stored_location_for(:admins) || "/admin/control_panel"
when Owner then stored_location_for(:owners) || "/client_accounts"
end
end

如果这没有帮助,我会在该方法的顶部放置一个调试器语句,以确保您的助手如“current_client”的行为符合您的预期(并确保完全调用 after_sign_in_path_for) .

关于ruby-on-rails - 无法登录用户资源 - devise/sessionsController#create 中的 NoMethod 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5808335/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com