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

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

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

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

Ruby on Rails 7

Ruby on Rails 7は、2021年12月に正式リリースされました。Ruby on Railsのバージョン7であり、フロントエンド開発環境を大幅に刷新。Node.jsを用いない構成がデフォルトになっています。

Q&A

解決済

1回答

437閲覧

パスワード変更なしのユーザー編集ができません。

退会済みユーザー

退会済みユーザー

総合スコア0

Devise

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

Ruby on Rails 7

Ruby on Rails 7は、2021年12月に正式リリースされました。Ruby on Railsのバージョン7であり、フロントエンド開発環境を大幅に刷新。Node.jsを用いない構成がデフォルトになっています。

0グッド

0クリップ

投稿2022/10/11 21:12

編集2022/10/11 21:17

前提

パスワードの変更なしで更新できるようにしたいが、パスワードを入力してくださいとエラーメッセージが出てしまいます。
パスワードを変更せずに更新させたいのですが、記述したコードがうまく機能しません。

イメージ説明

ファイル

edit.html.erb

1<div class="user-edit"> 2 <h2><%= t('.アカウント設定/プロフィール設定', resource: resource.model_name.human) %></h2> 3 4 <%= form_for(resource, as: resource_name, url: registration_path(resource_name), html: { method: :put }) do |f| %> 5 <%= render "devise/shared/error_messages", resource: resource %> 6 7 <div class="field"> 8 <%= f.label "自身のイメージ写真" %><br /> 9 <%= f.file_field :image, class: 'image' %> 10 </div> 11 12 <div class="field"> 13 <%= f.label "自己紹介" %><br /> 14 <%= f.text_area :self_introduction, autofocus: true, autocomplete: ":self_introduction" %> 15 </div> 16 17 <div class="field"> 18 <%= f.label "名前" %><br /> 19 <%= f.text_field :name, autofocus: true, autocomplete: "name" %> 20 </div> 21 22 <div class="field"> 23 <%= f.label "フリガナ" %><br /> 24 <%= f.text_field :furiganaName, autofocus: true, autocomplete: "furiganaName" %> 25 </div> 26 27 <div class="field"> 28 <%= f.label "メールアドレス" %><br /> 29 <%= f.email_field :email, autofocus: true, autocomplete: "email" %> 30 </div> 31 32 <div class="field"> 33 <%= f.label "郵便番号" %><br /> 34 <%= f.text_field :post_code, autofocus: true, autocomplete: "post_code" %> 35 </div> 36 37 <div class="field"> 38 <%= f.label "住所" %><br /> 39 <%= f.text_field :address, autofocus: true, autocomplete: "address" %> 40 </div> 41 42 <% if devise_mapping.confirmable? && resource.pending_reconfirmation? %> 43 <div><%= t('.currently_waiting_confirmation_for_email', email: resource.unconfirmed_email) %></div> 44 <% end %> 45 46 <div class="field"> 47 <%= f.label :password %> <i>(<%= t('.変更したくない場合は空白のままにしてください') %>)</i><br /> 48 <%= f.password_field :password, autocomplete: "new-password" %> 49 <% if @minimum_password_length %> 50 <br /> 51 <em><%= t('devise.shared.minimum_password_length', count: @minimum_password_length) %></em> 52 <% end %> 53 </div> 54</div> 55 56 <div class="field"> 57 <%= f.label :password_confirmation %><br /> 58 <%= f.password_field :password_confirmation, autocomplete: "new-password" %> 59 </div> 60 61 <% if false %> 62 <div class="field"> 63 <%= f.label :current_password %> <i>(<%= t('.we_need_your_current_password_to_confirm_your_changes') %>)</i><br /> 64 <%= f.password_field :current_password, autocomplete: "current-password" %> 65 </div> 66 <% end %> 67 68 <div class="actions"> 69 <%= f.submit "更新" %> 70 </div><br /> 71<% end %> 72 73 74<%#= link_to "退会", users_hide_path(current_user), method: :put, "data-confirm" => "本当に退会しますか?", class: "btn btn-outline-danger" %> 75<%= link_to '戻る', :back, class:"btn btn-primary btn-lg " %> 76</div>

registrations_controller.rb

1class Users::RegistrationsController < Devise::RegistrationsController 2 # before_action :configure_sign_up_params, only: [:create] 3 # before_action :configure_account_update_params, only: [:update] 4 before_action :configure_account_update_params, only: [:update] 5 # GET /resource/sign_up 6 # def new 7 # super 8 # end 9 10 # POST /resource 11 # def create 12 # super 13 # end 14 15 # GET /resource/edit 16 # def edit 17 # super 18 # end 19 20 # PUT /resource 21 # def update 22 # super 23 # end 24 25 # DELETE /resource 26 # def destroy 27 # super 28 # end 29 30 # GET /resource/cancel 31 # Forces the session data which is usually expired after sign 32 # in to be expired now. This is useful if the user wants to 33 # cancel oauth signing in/up in the middle of the process, 34 # removing all OAuth session data. 35 # def cancel 36 # super 37 # end 38 39 protected 40 def update_resource(resource, params) 41 resource.update_without_current_password(params) 42 end 43 44 def after_update_path_for(_resource) 45 uers_path 46 end 47 48 def configure_account_update_params 49 devise_parameter_sanitizer.permit(:account_update, keys: [:name, :image, :self_introduction, :email, :furiganaName, :telephone_number, :address, :post_code]) 50 end 51 52 def after_update_path_for(resource) 53 user_path(@user.id) 54 end 55end

user.rb

1class User < ApplicationRecord 2 # Include default devise modules. Others available are: 3 # :confirmable, :lockable, :timeoutable, :trackable and :omniauthable 4 devise :database_authenticatable, :registerable, 5 :recoverable, :rememberable, :validatable 6 7 8 9 validates :name, presence: true, length: { maximum: 50 } 10 validates :email, presence: true, length: { maximum: 255 }, 11 format: { with: /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i }, 12 uniqueness: { case_sensitive: false } 13 validates :furiganaName, presence: true, length: { maximum: 100 }, 14 format: { with: /\A[ァ-ヴー]+\z/u } 15 validates :telephone_number, numericality: { only_integer: true }, length: { maximum:11 }, 16 format: { with: /\A[0-9]+\z/ } 17 validates :post_code, presence: true, format:{with: /\A\d{3}[-]?\d{4}\z/} 18 validates :password, presence: true, length: { minimum: 6, maximum: 10 } 19 validates :self_introduction, length: { maximum: 1000 } 20 mount_uploader :image, ImageUploader 21 22 has_many :plans, dependent: :destroy 23 has_many :relationships 24 has_many :followings, through: :relationships, source: :follow 25 has_many :reverses_of_relationship, class_name: 'Relationship', foreign_key: 'follow_id' 26 has_many :followers, through: :reverses_of_relationship, source: :user 27 has_many :favorites, dependent: :destroy 28 has_many :favorite_plans, through: :favorites, source: :plan 29 has_many :entries, dependent: :destroy 30 has_many :messages, dependent: :destroy 31 has_many :rooms 32 33 def follow(other_user) 34 unless self == other_user 35 self.relationships.find_or_create_by(follow_id: other_user.id) 36 end 37 end 38 39 def unfollow(other_user) 40 relationship = self.relationships.find_by(follow_id: other_user.id) 41 relationship.destroy if relationship 42 end 43 44 def following?(other_user) 45 self.followings.include?(other_user) 46 end 47 48 def favorite(plan) 49 self.favorites.find_or_create_by(plan_id: plan.id) 50 end 51 52 def unfavorite(plan) 53 favorite = self.favorites.find_by(plan_id: plan.id) 54 favorite.destroy if favorite 55 end 56 57 def is_favorite?(plan) 58 self.favorite_plans.include?(plan) 59 end 60 61 def update_without_current_password(params, *options) 62 params.delete(:current_password) 63 64 if params[:password].blank? && params[:password_confirmation].blank? 65 params.delete(:password) 66 params.delete(:password_confirmation) 67 end 68 69 result = update(params, *options) 70 clean_up_passwords 71 result 72 end 73end

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

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

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

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

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

guest

回答1

0

ベストアンサー

deviseを使う場合、パスワードの条件設定はdevise側で行ってください。

パスワードの変更を行わない場合、モデルのpasswordnilとなるので、validatesに引っかかってしまいます。

投稿2022/10/11 23:14

maisumakun

総合スコア145184

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

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

退会済みユーザー

退会済みユーザー

2022/10/12 03:31

バリデーションのパスワードを2重にかけることで編集することができました。 本当にありがとうございます! validates :password, presence: true, format: { with: VALID_PASSWORD_REGEX }, length: { minimum: 6, maximum: 10 }, on: :create validates :password, format: { with: VALID_PASSWORD_REGEX }, length: { minimum: 6, maximum: 10 }, allow_blank: true, on: :update
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問