[英]Rails with Devise Token Auth uninitialized constant Users
I am using rails with devise token auth.我正在使用带有设计令牌身份验证的导轨。 i want to implement omniauth but it keeps me asking uninitialized constant user我想实现 omniauth,但它让我一直问未初始化的常量用户
routes.rb路由文件
namespace :api, defaults: { format: :json } do
scope :v1 do
mount_devise_token_auth_for 'User', at: 'auth', controllers: { omniauth_callbacks: 'users/omniauth_callbacks' }
end
end
in app/controllers/api/v1/users/omniauth_callbacks_controller.rb在 app/controllers/api/v1/users/omniauth_callbacks_controller.rb
class OmniauthCallbacksController < DeviseTokenAuth::OmniauthCallbacksController
def google_oauth2
@user = User.from_omniauth(request.env["omniauth.auth"])
if @user.persisted?
sign_in @user, :event => :authentication #this will throw if @user is not activated
set_flash_message(:notice, :success, :kind => "Google") if is_navigational_format?
else
session["devise.google_data"] = request.env["omniauth.auth"]
end
redirect_to '/'
end
end
i dont know where i am doing wrong.我不知道我哪里做错了。 i have try all answer in google but not work我已经在谷歌中尝试了所有答案,但没有用
Is it Users
or User
constant that is unitialized in the error message?错误消息中未初始化的是Users
还是User
常量?
Let's cover both.让我们涵盖两者。
In your routes.rb
you specified controller path as 'users/omniauth_callbacks'
.在您的routes.rb
您将控制器路径指定为'users/omniauth_callbacks'
。 By convention this means your controller is called Users::OmniauthCallbacksController
.按照惯例,这意味着您的控制器称为Users::OmniauthCallbacksController
。 Notice the Users
scope.注意Users
范围。
Now you can either drop the 'users' part of the route and move the controller up a folder or create the controller inside Users
scope.现在,您可以删除路由的“用户”部分并将控制器向上移动一个文件夹或在Users
范围内创建控制器。
class Users
class OmniauthCallbacksController < DeviseTokenAuth::OmniauthCallbacksController
...
end
end
User
is simply the model that is usually used to store devise data. User
只是通常用于存储设计数据的模型。 Make sure you have one in your /app/models
.确保您的/app/models
有一个。
class User < ApplicationRecord
devise :omniauthable, omniauth_providers: %i[google]
end
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.