Ruby on rails 密码确认不与密码进行比较

Ruby on rails 密码确认不与密码进行比较,ruby-on-rails,ruby,validation,passwords,Ruby On Rails,Ruby,Validation,Passwords,我不知道如何验证confirm password元素,这段代码似乎可以工作,但是validates confirmation of password似乎没有像预期的那样比较密码,只是需要一只手来习惯Rails 4的新方式 注册视图: 报名 <%= form_for @user do |f| %> <% if @user.errors.any? %> <div class="error_messages">

我不知道如何验证confirm password元素,这段代码似乎可以工作,但是validates confirmation of password似乎没有像预期的那样比较密码,只是需要一只手来习惯Rails 4的新方式

注册视图: 报名

    <%= form_for @user do |f| %>
        <% if @user.errors.any? %>
            <div class="error_messages">
                <h2>Form is invalid</h2>
                <ul>
                    <% for message in @user.errors.full_messages %>
                        <li><%= message %></li>
                    <% end %>
                </ul>
            </div>
        <% end %>
        <%= f.text_field :email, class: "form-control", autofocus: "", required: "", placeholder: "Email address" %>
        <%= f.password_field :password, class: "form-control", required: "", placeholder: "Password" %>
        <%= f.password_field :password_confirmation, class: "form-control", required: "", placeholder: "Confirm Password" %><br/>
        <p class="button"><%= submit_tag "Register", class: "btn btn-lg btn-primary btn-block" %></p>
    <% end %>
</div>

表格无效

用户控制器:

class UsersController < ApplicationController
    def new
        @user = User.new
    end

    def create
        @user = User.create(user_params)
        if @user.save
            redirect_to root_url, :notice => "Signed up!"
        else
            render "new"
        end
    end
    private
    def user_params
        params.require(:user).permit(:username, :email, :password, :passord_confirmation, :salt, :encrypted_password)
    end
end
class UsersController“已注册!”
其他的
呈现“新”
结束
结束
私有的
def用户参数
参数require(:user).permit(:用户名,:电子邮件,:密码,:密码确认,:salt,:加密密码)
结束
结束
用户模型:

class User < ActiveRecord::Base

    attr_accessor :password
    before_save :encrypt_password

    validates_presence_of :password, on: :create
    validates :password, confirmation: :true
    validates_presence_of :email
    validates_uniqueness_of :email

    def self.authenticate(email, password)
        user = find_by_email(email)
        if user && user.password_hash == BCrypt::Engine.hash_secret(password, user.password_salt)
            user
        else
            nil
        end
    end
    private
    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
class用户
除非您有自己的私人方法进行确认,否则您的型号密码确认功能可能会关闭

尝试将您的型号从
validates:password,confirmation::true
更改为
validates\u confirmation\u of:password


也有一些示例。

您需要使密码和密码确认可访问。例如:

attr_accessor :password
attr_accessible :password, :password_confirmation

validates :password, :presence     => true,
                     :confirmation => true

试着添加它,看看它是否有区别。

问题是什么,你的问题是什么?问题是rails 4没有通过比较两个密码来确认密码。问题是你如何修复它?你能更具体一点吗?“不确认”是什么意思?具体操作会发生什么,具体操作的实际结果是什么,期望的结果是什么?我之前有过这个,但看到你也可以像我这样做,但这也不起作用。你能尝试上面的更改并添加
验证存在密码确认,如果::password\u changed?
?这在尝试创建时只会给我一个这样的错误:未定义的方法'password\u changed',对于#它是Rails 4,属性_accessible不再有效。现在使用强参数。