2011-04-27 44 views
4

我遇到了Rails 3和Devise的一个特殊问题。我有我的Rails应用程序3的用户类型:
客户,业主和管理员无法登录用户资源 - 设计/会话控制器中的NoMethod错误#创建

的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> 

应用程序跟踪为空。

回答

8

根据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 

如果这样做没有帮助,我就放了debugger语句在方法的顶部,以确保为您希望您的助手像“current_client”的行为(并确保after_sign_in_path_for被调用)。

+0

现在,这就是我所说的SWEEEEETTT !!! :) – 2011-04-27 20:05:43

+0

真实的故事伴侣,甜蜜。就像他说的^^ – 2011-12-23 22:23:42

0

只是一个想法,但如果你改变的响应会发生什么: “/client_accounts/#{current_client.id.to_s}”

什么是您的耙路线造成什么样子的?