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し、次のように表示します -

routes.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 に答える