【发布时间】:2015-08-04 06:41:48
【问题描述】:
我正在尝试使用 Mongoid、devise、devise_token_auth 和 ng-token-auth 为使用 Mongoid 和 Angular 作为客户端的 Rails 编写的 API 进行基于令牌的授权。
问题是当我按照步骤安装devise_token_auth 时,我在重新启动我的Rails 应用程序时收到错误:undefined methodtable_exists?'对于用户:类`
我假设因为我使用的是 Mongoid,所以 User 类没有 table_exists? 方法。
我该如何解决这个问题?或者,更重要的是,我怎样才能让它发挥作用?
编辑:这是我的用户类
class User
include Mongoid::Document
include Mongoid::Timestamps
include Mongoid::Enum
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
## Database authenticatable
field :email, type: String, default: ""
field :encrypted_password, type: String, default: ""
## Recoverable
field :reset_password_token, type: String
field :reset_password_sent_at, type: Time
## Rememberable
field :remember_created_at, type: Time
## Trackable
field :sign_in_count, type: Integer, default: 0
field :current_sign_in_at, type: Time
field :last_sign_in_at, type: Time
field :current_sign_in_ip, type: String
field :last_sign_in_ip, type: String
## Confirmable
field :confirmation_token, type: String
field :confirmed_at, type: Time
field :confirmation_sent_at, type: Time
field :unconfirmed_email, type: String # Only if using reconfirmable
include DeviseTokenAuth::Concerns::User
attr_accessor :reset_token
enum :role, [:admin, :author]
after_initialize :set_default_role, :if => :new_record?
before_create :set_auth_token
field :first_name, type: String
field :last_name, type: String
field :domain, type: String
field :payment_details, type: Hash
field :subscriber, type: Boolean
field :stripe_details, type: Hash
field :theme, type: String
# Validation
VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-]+(?:\.[a-z\d\-]+)*\.[a-z]+\z/i
before_save { self.email = email.downcase }
before_create :create_remember_token
# Get rid of devise-token_auth issues from activerecord
def table_exists?
true
end
def columns_hash
# Just fake it for devise-token-auth; since this model is schema-less, this method is not really useful otherwise
{} # An empty hash, so tokens_has_json_column_type will return false, which is probably what you want for Monogoid/BSON
end
def set_default_role
self.role ||= :admin
end
end
编辑 2:添加堆栈跟踪
【问题讨论】:
-
在你的 gemfile 中包含这个 ``` gem 'rails', '~> 5.1.4' gem 'mongoid', '~> 6.2', '>= 6.2.1' gem 'devise_token_auth' , git: 'github.com/BunHouth/devise_token_auth.git', branch: 'mongoid' gem 'mongoid-locker', '~> 0.3.4' ``` 运行
bundle install并遵循 master 分支配置。它对我有用。
标签: ruby-on-rails angularjs devise