我安装了devise令牌auth gem。https://github.com/lynndylanhurley/devise_token_auth#model-concerns
我设法得到路线和所有。但是很遗憾,注册已停止。
当我尝试注册时,它显示出这样的错误。
NoMethodError in Devise::RegistrationsController#create
undefined method `provider' for #<User:0x007f49fd5da2e8>
Extracted source (around line #433):
else
match = match_attribute_method?(method.to_s)
match ? attribute_missing(match, *args, &block) : super
end
end
这是我的路线
devise_for :admin_users, ActiveAdmin::Devise.config
ActiveAdmin.routes(self)
devise_for :users
namespace :api do
scope :v1 do
mount_devise_token_auth_for 'User', at: 'auth'
end
end
根据文档,注册需要电子邮件,密码和password_confirmation。我不是我在这里想念的。
这是“用户”的架构
create_table "users", force: :cascade do |t|
t.string "email", default: "", null: false
t.string "encrypted_password", default: "", null: false
t.string "reset_password_token"
t.datetime "reset_password_sent_at"
t.datetime "remember_created_at"
t.integer "sign_in_count", default: 0, null: false
t.datetime "current_sign_in_at"
t.datetime "last_sign_in_at"
t.string "current_sign_in_ip"
t.string "last_sign_in_ip"
t.datetime "created_at"
t.datetime "updated_at"
t.string "authentication_token"
t.string "provider", null: false
t.string "uid", default: "", null: false
t.string "confirmation_token"
t.datetime "confirmed_at"
t.datetime "confirmation_sent_at"
t.string "unconfirmed_email"
t.string "name"
t.string "nickname"
t.string "image"
t.string "tokens"
end
add_index "users", ["authentication_token"], name: "index_users_on_authentication_token", using: :btree
add_index "users", ["email"], name: "index_users_on_email", unique: true, using: :btree
add_index "users", ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true, using: :btree
add_index "users", ["uid", "provider"], name: "index_users_on_uid_and_provider", unique: true, using: :btree
这是用户模型
class User < ActiveRecord::Base
# Include default devise modules.
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable,
:confirmable, :omniauthable
include DeviseTokenAuth::Concerns::User
has_many :movies
has_many :reviews
end
这是我用于创建用户帐户的POST请求?
{
"user": {
"email": "[email protected]",
"password": "password12345678",
"password_confirmation": "password12345678" }
}
回应是这样的。
{"status":"error","errors":["Please submit proper sign up data in request body."]}
这是相应请求的日志。
Started POST "/api/v1/auth/" for 127.0.0.1 at 2015-05-28 15:51:28 +0530
Processing by DeviseTokenAuth::RegistrationsController#create as */*
Parameters: {"user"=>{"email"=>"[email protected]", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]"}, "registration"=>{"user"=>{"email"=>"[email protected]", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]"}}}
Can't verify CSRF token authenticity
Unpermitted parameters: user, registration
Filter chain halted as :validate_sign_up_params rendered or redirected
Completed 422 Unprocessable Entity in 4ms (Views: 0.3ms | ActiveRecord: 0.0ms)
该错误背后的原因是什么,我该如何解决?
我设法设法找出了实现中缺少的内容。当我使用电子邮件身份验证时,设计令牌身份验证gem需要uid
一个和provider
。
当我们尝试发布不带那些参数的请求时,由于参数验证,它将失败。要绕过它,我们可以在用户方法中生成uid
和provider
使用before_validation
。
这里我用email
的provider
,如果provider
是空白的,并为uid
我用email
,如果它是空白。
这就是我在用户模型中写的。
before_validation :set_provider
before_validation :set_uid
def set_provider
self[:provider] = "email" if self[:provider].blank?
end
def set_uid
self[:uid] = self[:email] if self[:uid].blank? && self[:email].present?
end
通过这种方式,您将获得用户注册所需的参数。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句