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

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

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

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

Ruby

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

Model

MVCモデルの一部であるModelはアプリケーションで扱うデータとその動作を管理するために扱います。

Ruby on Rails

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

HTML

HTMLとは、ウェブ上の文書を記述・作成するためのマークアップ言語のことです。文章の中に記述することで、文書の論理構造などを設定することができます。ハイパーリンクを設定できるハイパーテキストであり、画像・リスト・表などのデータファイルをリンクする情報に結びつけて情報を整理します。現在あるネットワーク上のほとんどのウェブページはHTMLで作成されています。

Q&A

2回答

725閲覧

【Rails】Devise アカウント編集 パスワードなしで更新

退会済みユーザー

退会済みユーザー

総合スコア0

Devise

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

Ruby

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

Model

MVCモデルの一部であるModelはアプリケーションで扱うデータとその動作を管理するために扱います。

Ruby on Rails

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

HTML

HTMLとは、ウェブ上の文書を記述・作成するためのマークアップ言語のことです。文章の中に記述することで、文書の論理構造などを設定することができます。ハイパーリンクを設定できるハイパーテキストであり、画像・リスト・表などのデータファイルをリンクする情報に結びつけて情報を整理します。現在あるネットワーク上のほとんどのウェブページはHTMLで作成されています。

0グッド

0クリップ

投稿2020/05/12 00:25

編集2020/05/13 03:47

RailsのDeviseを使って、アカウント編集の実装を行っています。
以下の内容を参考に実装しているのですが、現在のパスワードの入力は不要になりましたが、毎回パスワード変更を行わないと正しく更新されないようになってしまいます。
https://kossy-web-engineer.hatenablog.com/entry/2018/11/06/102047

イメージ説明
上記のようにパスワードの変更を行わないとエラーが起きてしまいます。
https://kossy-web-engineer.hatenablog.com/entry/2018/11/06/102047

#registrations/edit.html.haml(現在のパスワードは入力なしでも更新できます。) .useredits .useredits-line .useredits-line_prof .useredits-line_prof_main .useredits-line_prof_main_user .useredits-line_prof_main_user_into -# %h2.useredits-line_prof_main_user_into_acount -# アカウント編集 -# Edit #{resource_name.to_s.humanize} = form_for(resource, as: resource_name, url: registration_path(resource_name), html: { method: :put }) do |f| = render "devise/shared/error_messages", resource: resource .edituserid .edituserid_sub %h2 ユーザID .edituserid_main = f.text_field :useruserid, autofocus: true, placeholder: "ユーザID" .editusername .editusername_sub = f.label :ユーザ名 .editusername_main = f.text_field :name, autofocus: true, placeholder: "ユーザ名" .editusermail .editusermail_sub = f.label :メールアドレス .editusermail_main = f.email_field :email, autofocus: true, autocomplete: "email", placeholder: "メールアドレス" - if devise_mapping.confirmable? && resource.pending_reconfirmation? %div Currently waiting confirmation for: #{resource.unconfirmed_email} .edituserform_password .edituserform_password_sub = f.label :パスワード .edituserform_password_main = f.password_field :password, autocomplete: "new-password", placeholder: "パスワード" .edituserform_passconf .edituserform_passconf_sub = f.label :パスワード確認 .edituserform_passconf_main = f.password_field :password_confirmation, autocomplete: "new-password", placeholder: "パスワード確認" .edituserform_passcurent .edituserform_passcurent_sub = f.label :現在のパスワード .edituserform_passcurent_main = f.password_field :current_password, autocomplete: "current-password", placeholder: "現在のパスワード" .edituserform_submit .edituserform_submit_sub = f.submit "更新", { :class => 'submit-btnaa' } #user.rb class User < ApplicationRecord # Include default devise modules. Others available are: # :confirmable, :lockable, :timeoutable, :trackable and :omniauthable devise :database_authenticatable, :registerable, :recoverable, :rememberable, :validatable validates :name,:useruserid, presence: true #追記 # @([a-zA-Z0-9_-]) mount_uploader :image, ImageUploader validates :useruserid, presence: true, uniqueness: true, format: { with: /\A@[\w+\-.]+\z/i } validates :email, presence: true, uniqueness: true, format: { with: /\A[\w+\-.]+@[a-z\d\-.]+.[a-z]+\z/i } validates :password, presence: true, format: { with: /\A(?=.*?[a-z])(?=.*?\d)[a-z\d]{6,40}+\z/i } # validates :profile, length: { maximum: 200 } #追記 has_many :comments has_many :tweets, dependent: :destroy has_many :likes, dependent: :destroy has_many :liked_tweets, through: :likes, source: :tweet has_many :following_relationships, foreign_key: "follower_id", class_name: "Relationship", dependent: :destroy has_many :followings, through: :following_relationships has_many :follower_relationships, foreign_key: "following_id", class_name: "Relationship", dependent: :destroy has_many :followers, through: :follower_relationships # has_many :temps def following?(other_user) following_relationships.find_by(following_id: other_user.id) end def follow!(other_user) following_relationships.create!(following_id: other_user.id) end def unfollow!(other_user) following_relationships.find_by(following_id: other_user.id).destroy end def already_liked?(tweet) self.likes.exists?(tweet_id: tweet.id) end def self.search(search) if search User.where('name LIKE(?) or sex LIKE(?) ', "%#{search}%","%#{search}%") else User.all end end 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 binding.pry result = update_attributes(params, *options) clean_up_passwords result end end #routes.rb devise_for :users, controllers: { registrations: 'registrations' } #app/controllers/registrations_controller.rb class RegistrationsController < Devise::RegistrationsController protected def update_resource(resource, params) resource.update_without_current_password(params) end end #ApplicationController.rb class ApplicationController < ActionController::Base protect_from_forgery with: :exception before_action :authenticate_user! before_action :configure_permitted_parameters, if: :devise_controller? protected def configure_permitted_parameters devise_parameter_sanitizer.permit(:sign_up, keys: [:name,:useruserid]) devise_parameter_sanitizer.permit(:account_update, keys: [:name, :profile,:image,:sex,:age,:tall]) end def log_in(user) session[:user_id] = user.id end end

現在のパスワードは空でも更新できますが、パスワード変更なしの記事が見当たらなかったので質問させていただきました。原因特定できる記事もなかったためアドバイスまたは提案をお願いします

result = update_attributes(params, *options) clean_up_passwords result #上記の部分でエラーが起きていました。 エラー文は以下のものになります。 Unpermitted parameter: :useruserid (1.0ms) BEGIN ↳ app/models/user.rb:59 User Exists (0.6ms) SELECT 1 AS one FROM `users` WHERE `users`.`useruserid` = BINARY '@aaa' AND `users`.`id` != 1 LIMIT 1 ↳ app/models/user.rb:59 User Exists (11.9ms) SELECT 1 AS one FROM `users` WHERE `users`.`email` = BINARY 'aaa@gmail.com' AND `users`.`id` != 1 LIMIT 1 ↳ app/models/user.rb:59 (3.6ms) ROLLBACK ↳ app/models/user.rb:59
#元々あったregistrations/edit.html.haml <h2>Edit <%= resource_name.to_s.humanize %></h2> <%= form_for(resource, as: resource_name, url: registration_path(resource_name), html: { method: :put }) do |f| %> <%= render "devise/shared/error_messages", resource: resource %> <div class="field"> <%= f.label :email %><br /> <%= f.email_field :email, autofocus: true, autocomplete: "email" %> </div> <% if devise_mapping.confirmable? && resource.pending_reconfirmation? %> <div>Currently waiting confirmation for: <%= resource.unconfirmed_email %></div> <% end %> <div class="field"> <%= f.label :password %> <i>(leave blank if you don't want to change it)</i><br /> <%= f.password_field :password, autocomplete: "new-password" %> <% if @minimum_password_length %> <br /> <em><%= @minimum_password_length %> characters minimum</em> <% end %> </div> <div class="field"> <%= f.label :password_confirmation %><br /> <%= f.password_field :password_confirmation, autocomplete: "new-password" %> </div> <div class="field"> <%= f.label :current_password %> <i>(we need your current password to confirm your changes)</i><br /> <%= f.password_field :current_password, autocomplete: "current-password" %> </div> <div class="actions"> <%= f.submit "Update" %> </div> <% end %> <h3>Cancel my account</h3> <p>Unhappy? <%= button_to "Cancel my account", registration_path(resource_name), data: { confirm: "Are you sure?" }, method: :delete %></p> <%= link_to "Back", :back %>
#編集後(useruserid追加) エラー画面 ↳ app/models/user.rb:60 User Exists (0.4ms) SELECT 1 AS one FROM `users` WHERE `users`.`useruserid` = BINARY '@aaaa' AND `users`.`id` != 1 LIMIT 1 ↳ app/models/user.rb:60 User Exists (0.4ms) SELECT 1 AS one FROM `users` WHERE `users`.`email` = BINARY 'aaa@gmail.com' AND `users`.`id` != 1 LIMIT 1 ↳ app/models/user.rb:60 (0.2ms) ROLLBACK ↳ app/models/user.rb:60 result = update_attributes(params, *options) 上記のコードでエラーが起きました。

binding.pryを実行した結果
イメージ説明

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

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

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

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

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

guest

回答2

0

立て続けに失礼します。

エラーの内容を見るとUnpermitted parameter: :useruseridとありますので、
どうやらuseruseridというカラムがdeviseに許可されていないようです。

#ApplicationController.rb def configure_permitted_parameters devise_parameter_sanitizer.permit(:sign_up, keys: [:name,:useruserid]) devise_parameter_sanitizer.permit(:account_update, keys: [:name, :profile,:image,:sex,:age,:tall]) end

devise_parameter_sanitizer.permit(:account_update, keys: [:name, :profile,:image,:sex,:age,:tall])
:useruseridが抜けていますので、恐らくこれが原因かと思います。

:account_updateつまり更新処理の際に許可するパラメーターにuseruseridが無いということですね。

投稿2020/05/12 16:34

gnfreeworks

総合スコア306

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

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

退会済みユーザー

退会済みユーザー

2020/05/12 16:51

上記を追加し、エラー文自体は変わりましたが、やはりパスワードの変更が必須になってしまいます。編集して概要は追加しました。
gnfreeworks

2020/05/12 17:29

まだダメでしたか。 > result = update_attributes(params, *options) 上記のコードでエラーが起きているとのことですが、 gemの"pry-byebug"が入っているのでしたらこの直前にbinding.pryを入れてブラウザ更新して処理を止め、 コンソールから"rails c"でirbを呼び出して"params"と入れて実行してみてください。 その結果も記載いただけますか?
退会済みユーザー

退会済みユーザー

2020/05/12 23:57

やりたいこととして画像を記載しましたので認識違いが起きていないか確認をお願いします。rails cは実行してみました。
gnfreeworks

2020/05/13 03:30

すみません、私の指示が少し間違っておりました。 gemの"pry-byebug"が入っているのでしたらこの直前にbinding.pryを入れてブラウザ更新して処理を止め、コンソールを見ると処理が止まって入力待ちになっているので、そこで"params"と入れて実行した時の画面を頂きたいです。 問題が無ければハッシュでemailやpasswordなどの情報が取得出来るはずなのですが、そこで何かおかしなデータになっていないか確認取れればと思います。
退会済みユーザー

退会済みユーザー

2020/05/13 03:32

この直前というのはresult = update_attributes(params, *options)の前にbinding.pryをいれるということですか?
gnfreeworks

2020/05/13 03:35

はい、resultの行のすぐ上の行に記載して下さい。 gemの"pry-byebug"がインストール済みであれば使えるはずです。
gnfreeworks

2020/05/13 03:37

あと、このresultの書かれたファイルのコードは載せてないようですが、可能であればそのファイルも載せて頂いた方が皆さん分かりやすいと思います。
退会済みユーザー

退会済みユーザー

2020/05/13 03:47

はい、ファイル、binding.pryの内容追加記載しました。
退会済みユーザー

退会済みユーザー

2020/05/13 23:48

内容記載しましたので確認の程お願いします
gnfreeworks

2020/05/14 02:06

原因がよく分かりませんね... [:name, :profile,:image,:sex,:age,:tall,:useruserid]の項目の中でDBで入力必須のものはありますか?nilはダメなど。
guest

0

deviseの編集画面では、もともとパスワード変更を入力しなくても現在のパスワードだけ入力すれば更新が可能になっているはずです。作業の中でその辺りの変更を行なった記憶はありませんか?

投稿2020/05/12 16:28

gnfreeworks

総合スコア306

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

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

退会済みユーザー

退会済みユーザー

2020/05/12 16:47

あまり覚えがありません。元の作成したファイルを追加記載しましたので気づきがあれば返答をお願い致します。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだベストアンサーが選ばれていません

会員登録して回答してみよう

アカウントをお持ちの方は

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問