【问题标题】:has_secure_password password can't be blank rails 4.1has_secure_password 密码不能为空白 rails 4.1
【发布时间】:2015-02-02 05:53:28
【问题描述】:

我们在 rails 4.1 中实现了“has_secure_password”进行身份验证。代码如下:

    # == Schema Information
#
# Table name: users
#
#  id              :integer          not null, primary key
#  name            :string(255)
#  firstname       :string(255)
#  created_at      :datetime
#  updated_at      :datetime
#  password_digest :string(255)
#  lastname        :string(255)
#

(用户.rb)

class User < ActiveRecord::Base
  has_secure_password
end

(user_controllers.rb)

class UsersController < ApplicationController
  def create
    u = User.new(user_params)
    if u.save
      render json: u, status: 200 #, location: u
    else
      render json: {errors: u.errors.messages}, status: 422
    end
  end

  private
  def user_params
    params.require(:user).permit(:name, :firstname, :lastname, :password, :password_confirmation)
  end
end

向 /users 发送发布请求以创建一个时,我们收到“密码不能为空”错误。 另外,我们尝试显示user_params的内容,除了password和password_confirmation,其他的params都有。 有什么想法吗?

【问题讨论】:

  • 你能显示你发送的json吗?
  • 我们发送这个:{"name":"Dave", "firstname":"Rob", "lastname":"Bert", "password":"superpass", "password_confirmation": “超级通行证”}
  • 你能附上你的表格吗?

标签: ruby ruby-on-rails-4


【解决方案1】:

我们找到了问题所在,也许有一天它会对某人有所帮助。 我们发送了这个 Json:

{"name":"Dave", "firstname":"Rob", "lastname":"Bert", "password":"superpass", "password_confirmation":"superpass"}

框架期望这样:

{ "user" : {"name":"Dave", "firstname":"Rob", "lastname":"Bert", "password":"superpass", "password_confirmation":"superpass"}}

只是一开始就忘记了用户密钥...

【讨论】:

    猜你喜欢
    • 2017-12-07
    • 1970-01-01
    • 2014-09-06
    • 1970-01-01
    • 2015-03-15
    • 1970-01-01
    • 2014-04-15
    • 2015-01-21
    • 1970-01-01
    相关资源
    最近更新 更多