无法登录用户资源 - 设计中的NoMethod错误/ sessionsController#create [英] Unable to sign in for a user resource - NoMethod error in devise/sessionsController#create

查看:199
本文介绍了无法登录用户资源 - 设计中的NoMethod错误/ sessionsController#create的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在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)
如果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在Devise / sessionsController#create 
undefined方法`client_url'for#< Devise :: SessionsController:0x10a98f868>

应用程序跟踪为空。

解决方案

根据设计文档,after_sign_in_path_for方法接受实际的客户端对象,但是您将输入与一组字符串进行比较,这些字符串将返回false,因此您的big if子句将返回nil。不知道Devise是如何设计的,但是找到'client_url'将是一个合理的默认值。



尝试这样做:

  def after_sign_in_path_for(资源)
案例资源
当客户端然后/ client_accounts /+ current_client.id.to_s
当Admin然后stored_location_for(:admins)|| / admin / control_panel
当所有者然后存储_location_for(:所有者)|| / client_accounts
end
end

如果没有帮助,我会把一个调试器语句放在方法的顶部,以确保你的帮助者像'current_client'按照你的期望行为(并确保在所有调用after_sign_in_path_for)。


I'm having a peculiar problem with Rails 3 and Devise. I have 3 user types for my Rails App:
Clients, Owners and Admins

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

My Sign In's for Owners and Admins are working fine, but i'm unable to get the Clients sign in working.. Here's the error that i get:

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

Application trace is empty.

解决方案

According to the Devise docs the after_sign_in_path_for method takes the actual Client object, but you're comparing the input to a set of strings, which will all return false, so your big if clause will return nil. Not sure what Devise is designed to do when that happens, but looking for 'client_url' would be a reasonable default.

Try this instead:

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

If that doesn't help, I would put a debugger statement at the top of the method, to make sure your helpers like 'current_client' are behaving as you expect (and to make sure that after_sign_in_path_for is being called at all).

这篇关于无法登录用户资源 - 设计中的NoMethod错误/ sessionsController#create的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆