如何使用确认密码验证密码[Rails 3.2]

And*_*huk 0 ruby-on-rails-3

如何在rails 3.2中使用确认密码验证密码

我的代码不起作用

你可以告诉我的错误

我尝试了很多变化来改变控制器中的代码.

密码保存但未验证密码确认字段和密码字段.

请帮帮我,帮帮我)))

意见

<%= form_for :password, :url => { :action => "change_password" }, :id => @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.password_field :password %>
<%= f.password_field :password_confirmation %>
<%= f.submit "Save", :class => "button blue" %>
<% end %> 
Run Code Online (Sandbox Code Playgroud)

用户控制器

  def change_password
    @page_title = "Changing Zetfon account password"
    @user = current_user
    if request.post?

       @user.password = Digest::SHA1.hexdigest(params[:password][:password]) 
    if @user.save
        redirect_to :action => 'profile'
        flash[:status] = "Your password was changed. Next time you sign in use your new password."
      else
     flash[:status] = _('Your password not changed')
        render :action => "change_password"
      end
    end
  end
Run Code Online (Sandbox Code Playgroud)

用户模型

  validates_confirmation_of :password
  attr_accessible :password_confirmation
  attr_accessor :password
Run Code Online (Sandbox Code Playgroud)

小智 10

将以下行添加到模型中

    validates :password, confirmation: true
Run Code Online (Sandbox Code Playgroud)