Rails密码不能为空白错误

时间:2014-11-23 03:10:49

标签: ruby-on-rails ruby-on-rails-4

我正在尝试在Rails 4.1.6中创建用户注册表单。我一直收到'密码不能为空'的错误。我可以看到password和password_confirmation都在params哈希中,但不在params [:user]子哈希中。我无法弄清楚为什么我的生活。任何帮助将不胜感激。

用户模型:

class User < ActiveRecord::Base

    belongs_to :company

    has_secure_password

end

用户控制器:

class UsersController < ApplicationController

    respond_to :json

    def index
        @users = User.all
        respond_with(@users)
    end

    def create
        @user = User.new(user_params)
        @user.save
        respond_with(@user)
    end


    private

        def user_params
            params.require(:user).permit(:given_name, :family_name, :email, :password, :password_confirmation)
        end

end

1 个答案:

答案 0 :(得分:2)

听起来您的参数没有正确发送到服务器。

password应以user[password]的形式发送,password_confirmation应以user[password_confirmation]的形式发送。

请参阅hash and array parameters的文档。

或者,将wrap_parameters添加到控制器会将参数包装到嵌套哈希中。

wrap_parameters :user, include: [:given_name, :family_name, :email, :password, :password_confirmation]

请参阅ActionController::ParamsWrapper的文档。

相关问题