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) 上記のコードでエラーが起きました。
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
退会済みユーザー
2020/05/12 16:51
2020/05/12 17:29
退会済みユーザー
2020/05/12 23:57
2020/05/13 03:30
退会済みユーザー
2020/05/13 03:32
2020/05/13 03:35
2020/05/13 03:37
退会済みユーザー
2020/05/13 03:47
退会済みユーザー
2020/05/13 23:48
2020/05/14 02:06