设计:更改密码reset_password_token为空

时间:2014-08-05 22:02:06

标签: ruby-on-rails ruby ruby-on-rails-3 devise

我正在使用Devise&我正在尝试创建一个新用户可以重置密码的表单,但是我收到以下错误:

ActiveRecord::RecordNotUnique (PG::UniqueViolation: ERROR:  duplicate key value violates unique constraint "index_users_on_reset_password_token"

发生这种情况的原因是因为

生成了reset_password_token
<%= f.hidden_field :reset_password_token %> 

是空的。例如:

<input id="user_reset_password_token" name="user[reset_password_token]" type="hidden">

这是控制器动作

  def update_password
    @user = current_user
    # raise params.inspect
    if @user.update_with_password(params[:user])
      sign_in(@user, :bypass => true)
      redirect_to '/account_settings', :notice => "Your Password has been updated!"
    else
      redirect_to '/account_settings', :notice => "Your Password was not changed. Try again"
    end
  end

这是我的表格 的 edit_password.html.erb

<ul class="nav nav-pills">
  <li class="active"><a href="#">Change Password</a></li>
    <li><a href="#">Profile</a></li>
  <li><a href="#">Messages</a></li>
</ul>

<hr/>
<%= render :template => 'users/edit_password_form', 
                    :locals => { :resource => current_user, :resource_name => "user" } %>

edit_password_form.html.erb

<%= form_for(resource, :as => resource_name, :url => "password/users/update_password", :html => { :method => :put }) do |f| %>
<%# devise_error_messages! %>
<%= f.hidden_field :reset_password_token %>
<br>
<div class="row">
    <div class="col-sm-3 col-md-3 "> 
        <%= f.label :current_password, "Current Password:" %>   
    </div>
    <div class="col-sm-3 col-md-3 ">
        <%= f.password_field :current_password, :placeholder =>"Current Password" %>
    </div>  
</div>
<br>
<div class="row">
    <div class="col-sm-3 col-md-3 ">
        <%= f.label :password, "New Password:" %>
    </div>
    <div class="col-sm-3 col-md-3 "><%= f.password_field :password, :placeholder=>"New Password" %>
    </div>
</div>
<br>
<div class="row">
    <div class="col-sm-3 col-md-3 ">
        <%= f.label :password_confirmation, "Confirm New Password:" %>
    </div>
    <div class="col-sm-3 col-md-3 ">
        <%= f.password_field :password_confirmation, :placeholder=>"Confirm New Password" %>
    </div>
</div>
<br>
<br>
<%= f.submit "Change my password", :class => "btn btn-default" %>
<% end %>

1 个答案:

答案 0 :(得分:0)

我认为这与内置的设计功能无关,当他们忘记密码时,可以发送电子邮件重置密码。相反,看起来您允许他们输入当前密码然后选择一个新密码。在这种情况下,我认为您根本不需要表单中的reset_password_token,因为它无关紧要,即您可以删除该行:

<%= f.hidden_field :reset_password_token %>