我安装了devise token 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@g.com",
"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@g.com", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]"}, "registration"=>{"user"=>{"email"=>"email@g.com", "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)
该错误背后的问题是什么,我该如何解决?
答案 0 :(得分:7)
不知何故,我设法找出了实施中遗漏的内容。当我使用电子邮件身份验证时,设计令牌auth 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
使用select pep, (resi 10)
。
这是我在用户模型中写的。
a=10
cmd.select("pep", "resi " + str(a))
通过这种方式,您将获得用户注册所需的参数。
答案 1 :(得分:1)
我通过从用户subhash中取出用户参数来修复此问题。我正在写一个控制器测试
post :create, { email: "user@example.org", password: "password", password_confirmation: "password" }
工作和
post :create, {user: { email: "user@example.org", password: "password", password_confirmation: "password" }}
答案 2 :(得分:0)
您还可以使用Devise生成的令牌设置uid
。
Devise.friendly_token.first(10)