无法登录用户资源 - 设计/ sessionsController #create中的NoMethod错误

Jas*_*ngh 4 ruby ruby-on-rails devise

我对Rails 3和Devise有一个特殊的问题.我的Rails应用程序有3种用户类型:
客户端,所有者和管理员

routes.rb中:

   devise_for :admins
   devise_for :owners
   devise_for :clients 
Run Code Online (Sandbox Code Playgroud)

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
Run Code Online (Sandbox Code Playgroud)

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

NoMethodError in Devise/sessionsController#create
undefined method `client_url' for #<Devise::SessionsController:0x10a98f868>
Run Code Online (Sandbox Code Playgroud)

应用程序跟踪为空.

Pre*_*ids 8

根据Devise文档,after_sign_in_path_for方法接受实际的Client对象,但是您将输入与一组字符串进行比较,这些字符串都将返回false,因此您的big 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
Run Code Online (Sandbox Code Playgroud)

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