我完全是Rails的新手,我知道StackOverflow上的這個問題已經存在很多問題,但我嘗試了幾乎所有的解決方案,但沒有一個解決方案爲我工作。Rails中的密碼不能爲空(使用has_secure_password)
我想在我的rails項目中使用has_secure_password來實現身份驗證,我遵循了rails文檔中提到的所有步驟。 即使我在輸入框中輸入密碼並確認密碼值,我也會在提交創建用戶表單後收到「密碼不能爲空」錯誤消息。
請建議我是否缺少任何東西。
步驟我隨後添加下面線在寶石文件爲─ 1) - 寶石 'bcrypt',要求: 'bcrypt' 2)束安裝 3)我的模型代碼 -
class User < ActiveRecord::Base
validates :name, presence: true, uniqueness: true
has_secure_password
end
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>
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
你使用什麼bcrypt版本? – locoboy
3.1.7,甚至我沒有指定任何版本,嘗試它 –
你的'user_params'白名單'密碼'和'password_confirmation?' – AOG