0

form_for の単なる文字列である User モデルの profile 属性を変更したいと考えています。プロファイル属性を更新すると、パスワードが空白に設定されるという問題が発生しています。

つまり、ユーザー アカウントにはパスワードがありますが、プロファイルを変更すると、パスワードが空白になります。次回ログインするときは、パスワード フィールドを空白のままにします。

パスワードを変更せずにプロファイル フィールドを変更するにはどうすればよいですか?

ありがとう。

以下は私のフォームで、アクションを更新します。パスワードの検証と確認が必要かどうかのブール値であるupdateing_passwordという隠しフィールドを渡しています。

edit_profile.html.erb

<%= form_for @user, :html => { :multipart => true } do |f| %>

  <%= f.label :profile %>
  <%= f.text_area :profile%>

  <%= f.hidden_field :updating_password, :value => "false" %>

  <%= submit_tag "update" %>

<% end %>

users_controller.rb

def update
  @user = User.find(params[:id])
  @user.updating_password = params[:user][:updating_password]

  #for updating profile only
  if(@user.updating_password == 'false')
    if @user.update_attribute('profile', params[:user][:profile])
      redirect_to @user
    else
      @title = "Edit user"
      render 'edit'
     end
   end

  #for updating everything else including password
  if(@user.updating_password == 'true')
    if @user.update_attributes(params[:user])
      redirect_to @user
    else
      @title = "Edit user"
      render 'edit'
    end
  end
end

user.rb ファイルから関連する updates_password スニペットを追加します。

validates_presence_of :password, :if => :should_validate_password?
validates_confirmation_of :password, :if => :should_validate_password?

  def should_validate_password?
    updating_password || new_record?
  end
4

0 に答える 0