从头开始密码确认的铁路认证不起作用

时间:2014-11-18 17:01:39

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

我正在关注Railscasts #250 Authentication from Scratch,但问题是密码确认可能与密码不同,用户仍会被创建。

..模型/ dealer.rb:

class Dealer < ActiveRecord::Base
attr_accessor :password
before_save :encrypt_password

validates_confirmation_of :password
validates_presence_of :password, :on => :create

def encrypt_password
    if password.present?
        self.password_salt = BCrypt::Engine.generate_salt
        self.password_hash = BCrypt::Engine.hash_secret(password, password_salt)
    end
end
end

..控制器/ dealers_controller.rb

class DealersController < ApplicationController
def new
@dealer = Dealer.new
end
def create
@dealer = Dealer.new(dealer_params)

if @dealer.save
    redirect_to root_url, :notice => "Signed Up!"
else
    render "new"
end

end

    private
    def dealer_params
      params.require(:dealer).permit(:email, :password)
    end
    end

..视图/经销商/ new.html / ERB

<%= form_for @dealer do |f| %>

<p>
    <%= f.label :email %><br>
    <%= f.text_field :email %>
</p>

<p>
    <%= f.label :password %><br>
    <%= f.password_field :password %>
</p>

<p>
    <%= f.label :password_confirmation %><br>
    <%= f.password_field :password_confirmation %>
</p>
<p class="button"><%= f.submit %></p>

我需要做些什么来实现这个目标? Railscast评论中的两个人有同样的问题,但没有答案。

1 个答案:

答案 0 :(得分:0)

您是否曾尝试将password_confirmation添加到您允许的参数中,如下所示:

def dealer_params
  params.require(:dealer).permit(:email, :password, :password_confirmation)
end

如果这也无法尝试为password_confirmation生成访问器:

attr_accessor :password, :password_confirmation