0

我正在制作一个自定义密码编辑表单,我只更改密码。这是我的用户控制器代码:

  def change_my_password
    @user = User.find(current_user.id)
  end

  def update_my_password
    @user = User.find(current_user.id)
    #raise @user.inspect
    if @user.update_with_password(params[:user])
      sign_in @user, :bypass => true
      redirect_to users_path, :notice => "Password updated."
    else
      sign_in @user, :bypass => true
      render action: "change_my_password", :alert => "Unable to update user."
    end
  end

这是我的用户模型

class User < ActiveRecord::Base
  rolify
  # Include default devise modules. Others available are:
  # :token_authenticatable, :confirmable,
  # :lockable, :timeoutable and :omniauthable, :registerable,
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable

  # Setup accessible (or protected) attributes for your model
  attr_accessible :role_ids, :password, :password_confirmation, :username, :name, :email, :as => :admin
  attr_accessible :password, :password_confirmation, :username, :name, :email, :remember_me

  #attr_protected :username, :name, :email, :remember_me, :password, :password_confirmation


  validates_uniqueness_of :username
  validates_presence_of :username, :email

  validates_uniqueness_of :email
end

这是我的更改密码表格

= simple_form_for(@user, :url=>update_my_password_user_path(@user), :html => { :method => :put, :class => 'form-vertical' }) do |f|
  = f.error_notification
  = display_base_errors @user
  = f.input :password, :autocomplete => "off", :required => true
  = f.input :password_confirmation, :required => true
  = f.input :current_password, :hint => "we need your current password to confirm your changes", :required => true
  = f.button :submit, 'Update', :class => 'btn-primary'
= link_to "Back", :back

一切似乎都很好,但发生的情况是 - 如果我输入了错误的密码确认,则会提示我输入错误,但是当我再次提交表单时,我已退出并且密码不会更改。在我第一次提交表单以更改密码并确认密码错误时,它会从日志中将我注销。我不明白我哪里出错了 - 我什至输入了 sign_in 用户以避免必须退出,但它仍然无法正常工作。我在这里哪里出错了?

4

1 回答 1

1

使用post方法而不是put在路线中并查看如下 -

路线.rb -

resources "users" do
    collection do
      get 'change_my_password'
      post 'update_my_password'
    end
  end

change_my_password.html.erb -

  <%= form_for(@user, :url => { :action => "update_my_password" }, :html => {:method => "post"}) do |f| %>

  <%= f.text_field :password, :autocomplete => "off", :required => true %>
  <%= f.text_field :password_confirmation, :required => true %>
  <%= f.text_field :current_password, :hint => "we need your current password to confirm your changes", :required => true %>
  <%= f.submit 'Update', :class => 'btn-primary' %>
<%= link_to "Back", :back %>

<% end %>

这对我来说没有问题。

干杯!

于 2013-01-16T08:43:02.900 回答