Rails项目中未定义的Devise方法 [英] Devise methods undefined in Rails project

查看:168
本文介绍了Rails项目中未定义的Devise方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个安装了Devise宝石的Rails 5应用程序。我的项目是这样设置的,所有控制器都是命名空间...甚至Devise控制器。



在我的路由文件中我已经做到了(这将更好地解释我的命名空间结构):

  controllers:{
sessions:api / v1 / public / members / users / sessions ,
密码:api / v1 / public / members / users / passwords,
注册:api / v1 / public / members / users / registrations,
},
path_names:{
sign_in:'login',
密码:'忘记',
sign_up:'注册',
sign_out:'signout'
}

我还设置了设计视图文件,以反映所需的命名空间结构。



我在ApplicationController.rb中包含以下内容:

  before_action:authenticate_api_v1_public_members_user! 

由于我的Devise控制器具有命名空间,因此需要将标准的before_action:authenticate_user!方法更新为为了工作,上面的一个。



我遇到的问题是,我的任何视图中都没有一个Devise帮助器方法可用。在视图中调用'current_user'或'user_signed_in?'时,刷新浏览器时会出现未定义的方法错误。我尝试命名为这些帮助方法命名,但无济于事。



更新
我的应用程序中没有命名模式。我正在使用普通的用户模型。只有控制器是命名空间,并按照上面的路线进行结构化。



有没有人遇到类似的问题?

解决方案

似乎是Devise嵌套在您的routes.rb中的:api_v1_public_members 命名空间中。在这种情况下,它不仅会影响 authenticate_user!方法,将其更改为 authenticate_api_v1_public_members_user!,而且还会影响其他方法。




  • current_user 转为 current_api_v1_public_members_user


  • user_signed_in?转为 api_v1_public_members_user_signed_in? / code>




这里有类似的问题未定义的方法`authenticate_user! Api :: PostsController in Devise / Rails 4


I have a Rails 5 app with the Devise gem installed. My project is set up in such a way, that all controllers are namespaced...Even Devise controllers.

In my routes file I have done this(This will better explain my namespacing structure):

controllers: {
    sessions: "api/v1/public/members/users/sessions",
    passwords: "api/v1/public/members/users/passwords",
    registrations: "api/v1/public/members/users/registrations",
},
path_names: { 
  sign_in: 'login', 
  password: 'forgot', 
  sign_up: 'register',
  sign_out: 'signout'
}

I have also setup the devise view files to reflect the required namespaced structure.

I have included the following in my ApplicationController.rb:

before_action :authenticate_api_v1_public_members_user!

Since my Devise controllers are namespaced, the standard 'before_action :authenticate_user!' method needed to be updated to the one above in order to work.

The issue that I'm experiencing, is that none of the Devise helper methods are available in any of my views. When calling 'current_user' or 'user_signed_in?' in a view, I am presented with an 'undefined method' error when refreshing my browser. I tried namespacing these helper methods as well, but to no avail.

UPDATE Models are not namespaced in my app. I am using a normal User model. Only controllers are namespaced and are structured as indicated by the route above.

Does anybody have experience with a similar issue?

解决方案

It seems to be that Devise is nested within the :api_v1_public_members namespace in your routes.rb. In this case it affects not only on authenticate_user! method, changing it into authenticate_api_v1_public_members_user!, but on the others methods too.

  • current_user turns to current_api_v1_public_members_user

  • user_signed_in? turns to api_v1_public_members_user_signed_in?

There is similar problem here undefined method `authenticate_user! Api::PostsController in Devise / Rails 4

这篇关于Rails项目中未定义的Devise方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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