質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Devise

Deviseとは、Ruby-on-Railsの認証機能を追加するプラグインです。

Ruby

Rubyはプログラミング言語のひとつで、オープンソース、オブジェクト指向のプログラミング開発に対応しています。

Ruby on Rails

Ruby on Railsは、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

Q&A

解決済

1回答

4091閲覧

deviseのuser/editでpasswordだけが変更されない。

tat_23

総合スコア18

Devise

Deviseとは、Ruby-on-Railsの認証機能を追加するプラグインです。

Ruby

Rubyはプログラミング言語のひとつで、オープンソース、オブジェクト指向のプログラミング開発に対応しています。

Ruby on Rails

Ruby on Railsは、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

0グッド

0クリップ

投稿2019/09/02 07:14

前提・実現したいこと

ご覧いただきありがとうございます。

deviseで用意されたviewをカスタマイズしてユーザーがpasswordを変更できるようにしたく、質問させて頂きます。

発生している問題・ソースコード

認証機能にdeviseを使って自作アプリを作成しています。

user/edit(ユーザー情報の変更)画面でupdateする際に、current_passwordを入力しなくても更新できるようにしたいと考え、この記事を参考に、

class RegistrationsController < Devise::RegistrationsController protected def update_resource(resource, params) resource.update_without_password(params) end end

を追記し、routes.rbも編集して無事にcurrent_passwordなしで更新できるようになりました。

しかしこうすると、editページからpasswordが編集できないようになってしまうということをこのサイトで知ったので、このサイト通りに実装してみました。

しかし、他の要素(nameやemail)は問題なく更新されるにも関わらず、passwordだけは更新されずに困っています。

以下、私のソースコードです。

registrations_controller.rb

# frozen_string_literal: true class Users::RegistrationsController < Devise::RegistrationsController # before_action :configure_sign_up_params, only: [:create] before_action :configure_account_update_params, only: [:update] # GET /resource/sign_up # def new # super # end # POST /resource # def create # super # end # GET /resource/edit # def edit # super # end # PUT /resource # def update # super # end # DELETE /resource # def destroy # super # end # GET /resource/cancel # Forces the session data which is usually expired after sign # in to be expired now. This is useful if the user wants to # cancel oauth signing in/up in the middle of the process, # removing all OAuth session data. # def cancel # super # end protected # If you have extra params to permit, append them to the sanitizer. # def configure_sign_up_params # devise_parameter_sanitizer.permit(:sign_up, keys: [:attribute]) # end # If you have extra params to permit, append them to the sanitizer. def configure_account_update_params devise_parameter_sanitizer.permit(:account_update, keys: [:name]) devise_parameter_sanitizer.permit(:account_update, keys: [:attribute]) end def update_resource(resource, params) resource.update_without_current_password(params) end # The path used after sign up. def after_sign_up_path_for(resource) user_path(current_user) end # The path used after sign up for inactive accounts. # def after_inactive_sign_up_path_for(resource) # # my_page_path(resource) # # end end

app/models/user.rb

class User < ApplicationRecord # Include default devise modules. Others available are: # :confirmable, :lockable, :timeoutable, :trackable and :omniauthable # deviseで使うモジュールたち。 devise :database_authenticatable, :registerable, :recoverable, :rememberable, :validatable # 自分で追加したカラムにバリデーションをかけておく validates :name, presence: true, length: {maximum: 50} #ここから def update_without_current_password(params, *options) params.delete(:current_password) if params[:password].blank? && params[:password_confirmation].blank? params.delete(:password) params.delete(:password_confirmation) end result = update_attributes(params, *options) clean_up_passwords result end  #ここまで追記しました。 end

routes.rb

Rails.application.routes.draw do devise_for :users, :controllers => { :registrations => 'users/registrations', :sessions => 'users/sessions', :passwords => 'users/passwords' } devise_scope :user do get "/users/sign_out" => "devise/sessions#destroy" end resources :users end

app/views/devise/registrations/edit.html.erb(パスワードの部分とsubmitボタンの部分の抜粋)

<div class="form-group"> <%= f.password_field :password, autocomplete: "new-password" ,:placeholder => "password", id: "password", class: "form-control"%> <small class="form-text password_help_text">(6文字以上) パスワードの変更が不要な場合は空欄のままUpdateボタンを押してください</small> </div> <div class="form-group"> <%= f.password_field :password_confirmation, autocomplete: "new-password", id: "password_confirmation", :placeholder => "パスワードを再入力してください", class: "form-control"%> </div> <div class="text-right"> <%= f.submit "Update", class: "submit-button" %> </div>

該当すると思われるコードは記載させて頂きましたが、もし何か不足があれば教えて頂けますと幸いです。

丸一日使ってしまいましたがどうしても解決せず...

何か少しでも心当たりのある方がいらっしゃいましたら、どうかお力をお貸し頂けないでしょうか。

どうぞよろしくお願いします。

[追記]
update実行時のサーバーログです。

Started GET "/users/edit" for ::1 at 2019-09-01 07:51:37 +0900 Processing by Users::RegistrationsController#edit as HTML User Load (0.4ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? ORDER BY "users"."id" ASC LIMIT ? [["id", 3], ["LIMIT", 1]] Rendering devise/registrations/edit.html.erb within layouts/application Rendered devise/shared/_error_messages.html.erb (Duration: 0.2ms | Allocations: 15) Rendered devise/registrations/edit.html.erb within layouts/application (Duration: 3.8ms | Allocations: 591) Rendered layouts/_header.html.erb (Duration: 3.3ms | Allocations: 1255) Rendered layouts/_flash.html.erb (Duration: 0.1ms | Allocations: 17) Rendered layouts/_footer.html.erb (Duration: 5.9ms | Allocations: 2169) Completed 200 OK in 65ms (Views: 58.7ms | ActiveRecord: 0.4ms | Allocations: 12682) Started PUT "/users" for ::1 at 2019-09-01 07:52:08 +0900 Processing by Users::RegistrationsController#update as HTML Parameters: {"authenticity_token"=>"86/7SxntwXMG/OAbh+AAn68awpo1aVR+Od0jVNX6Y5dZxZYA5w3+Y0AG48nsNXeTZ9vPd2UFersMQvdCwHJt5A==", "user"=>{"name"=>"新しいユーザー", "email"=>"hogehoge2@gmail.com", "d"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]"}, "commit"=>"Update"} User Load (0.3ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? ORDER BY "users"."id" ASC LIMIT ? [["id", 3], ["LIMIT", 1]] User Load (0.3ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT ? [["id", 3], ["LIMIT", 1]] DEPRECATION WARNING: update_attributes is deprecated and will be removed from Rails 6.1 (please, use update instead) (called from update_without_current_password at /Users/#name/environment/products/book_give_app/app/models/user.rb:20) (0.1ms) begin transaction ↳ app/models/user.rb:20:in `update_without_current_password' User Update (0.6ms) UPDATE "users" SET "encrypted_password" = ?, "updated_at" = ? WHERE "users"."id" = ? [["encrypted_password", "$2a$11$1ZxOSTLooRXB7jAj4xyV1eyYUG1NZPQ93It8.AKYsVQvgGhgX22YG"], ["updated_at", "2019-08-31 22:52:08.883913"], ["id", 3]] ↳ app/models/user.rb:20:in `update_without_current_password' (2.0ms) commit transaction ↳ app/models/user.rb:20:in `update_without_current_password' Redirected to http://localhost:3000/ Completed 302 Found in 547ms (ActiveRecord: 3.2ms | Allocations: 187586)

パラメーターのところを見ると、passwordのところが何故か、"d"=>"[FILTERED]"になっていますが全く心当たりがありません。
何かご教授頂けますと幸いです。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

以下のように、StrongParameterに更新を許可するカラムを追加するとうまく行くかもです。

ruby

1 def configure_account_update_params 2 devise_parameter_sanitizer.permit(:account_update, keys: [:name]) 3 devise_parameter_sanitizer.permit(:account_update, keys: [:attribute]) 4 devise_parameter_sanitizer.permit(:account_update, keys: [:password]) #ここで更新を許可するカラムを設定 5 end

投稿2019/09/21 07:46

hatsu

総合スコア1809

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問