gpt4 book ai didi

ruby-on-rails - Authlogic 忽略密码参数

转载 作者:太空宇宙 更新时间:2023-11-03 18:21:41 25 4
gpt4 key购买 nike

Authlogic 在创建新用户时似乎忽略了密码参数。这是我的 users_controller 类:

class Api::V1::UsersController < ApplicationController

def create
@user = User.new(user_params)

respond_to do |format|
if @user.save
format.json { render :json => @user, :status => :created}
else
format.json { render :json => @user.errors, :status => :unprocessable_entity }
end
end
end

private
def user_params
params.require(:user).permit(:username, :email, :password)
end

end

还有我的用户模型:

class User < ActiveRecord::Base
acts_as_authentic do |c|
c.require_password_confirmation = false
end
end

当我使用用户名、电子邮件和密码参数向/api/v1/users/发送 POST 请求时,authlogic 说密码不能为空,即使它不是。这是 rails 打印出来的内容:

Started POST "/api/v1/users/" for 127.0.0.1 at 2013-06-22 00:03:30 -0400
Processing by Api::V1::UsersController#create as */*
Parameters: {"email"=>"someemail@website.com", "password"=>"[FILTERED]", "username"=>"myUser", "user"=>{"username"=>"myUser", "email"=>"someemail@website.com"}}
(0.2ms) BEGIN
User Exists (0.4ms) SELECT 1 AS one FROM "users" WHERE LOWER("users"."email") = LOWER('someemail@website.com') LIMIT 1
User Exists (0.2ms) SELECT 1 AS one FROM "users" WHERE LOWER("users"."username") = LOWER('myUser') LIMIT 1
User Exists (0.3ms) SELECT 1 AS one FROM "users" WHERE "users"."persistence_token" = '7b72bab3627914d33e83e4efe1c5a9dab190750efb227698c8b5b6be7a7ccf118160d8e12623078543e0f4e5f31eb30828799cb0d97fb2af195daee894c79902' LIMIT 1
(0.2ms) ROLLBACK
Completed 422 Unprocessable Entity in 33ms (Views: 0.2ms | ActiveRecord: 3.2ms)

我正在使用最新的 authlogic 和 Ruby 2/Rails 4。

最佳答案

看一下 Rails 日志的摘录:

{"email"=>"someemail@website.com", "password"=>"[FILTERED]", "username"=>"myUser", "user"=>{"username"=>"myUser", "email"=>"someemail@website.com"}}

看起来您发送的参数略有错误。要被 Authlogic 识别,password 参数应位于参数哈希中的 user 键下。 IE。 Rails 日志中的那一行应该如下所示(注意字符串的末尾):

{"email"=>"someemail@website.com", "password"=>"[FILTERED]", "username"=>"myUser", "user"=>{"username"=>"myUser", "email"=>"someemail@website.com", "password" => "[FILTERED]"}}

要修复它,您可以像这样进行破解:

private
def user_params
params.require(:user).permit(:username, :email).merge(:password => :password)
end

或者,您可以调整从客户端发送的参数(例如,在发送 HTTP 时使用 user[password] 参数名称而不是仅仅 password POST 请求)。

关于ruby-on-rails - Authlogic 忽略密码参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17247244/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com