Rails中的密码不能为空(使用has_secure_password)

Neh*_*war 3 ruby authentication ruby-on-rails

我对Rails完全陌生,我知道StackOverflow上的该问题已经存在许多问题,但是我尝试了几乎所有解决方案,但是没有一种解决方案对我有用。

我正在尝试使用has_secure_password在我的rails项目中实现身份验证,我遵循了rails文档中提到的所有步骤。提交创建用户表单后,即使我在输入框中输入密码并确认密码值,也收到“密码不能为空”错误消息。

请建议我是否有任何遗漏。

我遵循的步骤是-1)在gem文件的以下行中添加了-gem'bcrypt',要求:'bcrypt'2)捆绑安装3)我的模型代码-

class User < ActiveRecord::Base
  validates :name, presence: true, uniqueness: true
  has_secure_password
end
Run Code Online (Sandbox Code Playgroud)

4)我的查看代码-

<div>
          <%= f.label :name %>:
          <%= f.text_field :name, size: 40 %>
        </div>
        <div>
          <%= f.label :password, 'Password' %>:
          <%= f.password_field :password, size: 40 %>
        </div>
        <div>
          <%= f.label :password_confirmation, 'Confirm' %>:
          <%= f.password_field :password_confirmation, size: 40 %>
        </div>
        <div>
Run Code Online (Sandbox Code Playgroud)

5)我的控制器代码-

def create
    @user = User.new(user_params)

    respond_to do |format|
      if @user.save
        format.html { redirect_to users_url,notice: "User #{@user.name} was successfully created." }
        format.json { render :show, status: :created, location: @user }
      else
        format.html { render :new }
        format.json { render json: @user.errors, status: :unprocessable_entity }
      end
    end
  end
Run Code Online (Sandbox Code Playgroud)

Neh*_*war 5

非常感谢大家的帮助。我能够解决这个问题。

我在控制器中添加了以下代码行

 wrap_parameters :user, include: [:name, :password, :password_confirmation]
Run Code Online (Sandbox Code Playgroud)

文档 -http://api.rubyonrails.org/classes/ActionController/ParamsWrapper.html

这解决了我的问题。