概要
emailとパスワードの変更なしで更新できるようにしたいが、パスワードが無効とエラーが出る。
パスワードが変更せずに更新できるようにしているつもりが、うまく機能していない。
編集ページのエラーメッセージにて
1 error prohibited this user from being saved: Password is invalid
各ファイル
edit.html.erb
<h2>ユーザー編集</h2> <%= form_with model: @user, url: user_registration_path, class: 'registration-main', local: true do |f| %> <%= render "devise/shared/error_messages", resource: resource %> <div class="field"> <%= f.label :nickname %><br /> <%= f.text_field :nickname, placeholder:"例) ゲスト", maxlength:"12" %> </div> <div class="field"> <%= f.label :profile %><br /> <%= f.text_area :profile, placeholder:"例) エンジニア歴1年です!", maxlength:"200" %> </div> <div class="field"> <%= f.label "性別" %><br /> <%= f.collection_select(:gender_id, Gender.all, :id, :name, {}, {class:"gender-select"}) %> </div> <div class="field"> <%= f.label "年齢" %><br /> <%= f.collection_select(:area_id, Area.all, :id, :name, {}, {class:"gender-select"}) %> </div> <div class="field"> <%= f.label "写真" %><br /> <div id="image-list"></div> <%= f.file_field :image %> </div> <div class="actions"> <%= f.submit "更新" %> </div> <% end %>
registrations_controller.rb
class Users::RegistrationsController < Devise::RegistrationsController before_action :configure_account_update_params, only: [:update] before_action :ensure_normal_user, only: %i[update destroy] def ensure_normal_user redirect_to root_path, alert: 'ゲストユーザーは削除・更新できません。' if resource.email == 'guest@example.com' end protected def update_resource(resource, params) resource.update_without_password(params) end def after_update_path_for(_resource) rooads_path end def configure_account_update_params devise_parameter_sanitizer.permit(:account_update, keys: [:nickname, :area_id, :gender_id, :profile, :image]) end end
user_controller.rb
class UsersController < ApplicationController def update @user = User.find(params[:id]) if @user.valid? @user.update(update_params) redirect_to user_path(@user.id) else render :edit end end private def user_params params.require(:user).permit(:nickname, :gender_id, :profile, :image, :area_id, :email, :password, :password_confirmation) end def update_params params.require(:user).permit(:nickname, :gender_id, :profile, :image, :area_id) end end
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 with_options presence: true do validates :nickname end validates :password, presence: true, on: :create VALID_PASSWORD_REGEX = /\A(?=.*?[a-z])(?=.*?\d)[a-z\d]{6,100}+\z/i.freeze validates :password, format: { with: VALID_PASSWORD_REGEX } extend ActiveHash::Associations::ActiveRecordExtensions belongs_to :gender belongs_to :area has_one_attached :image has_many :likes def self.guest find_or_create_by!(email: 'guest@example.com') do |user| user.password = SecureRandom.urlsafe_base64 user.nickname = 'ゲスト' end end def liked_by?(rooad_id) likes.where(rooad_id: rooad_id).exists? 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 result = update_attributes(params, *options) clean_up_passwords result end end
routes
Rails.application.routes.draw do devise_for :users, controllers: { registrations: 'users/registrations', passwords: 'users/passwords' } devise_scope :user do post '/users/guest_sign_in', to: 'users/sessions#guest_sign_in' end root to: 'static_pages#top' resources :rooads post 'like/:id' => 'likes#create', as: 'create_like' delete 'like/:id' => 'likes#destroy', as: 'destroy_like' end
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2021/09/24 08:19