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

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

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

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

ファイル

ファイルとは、文字列に基づいた名前又はパスからアクセスすることができる、任意の情報のブロック又は情報を格納するためのリソースです。

Ruby on Rails

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

パスワード

パスワードは主に情報にアクセスする際に扱われます。主に、アクセス可能なユーザーを限定する手段として使われます。

HTML

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

Q&A

解決済

1回答

1682閲覧

【Rails/Devise】パスワード変更なしのユーザー編集ができない

mokosamejima

総合スコア11

Devise

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

ファイル

ファイルとは、文字列に基づいた名前又はパスからアクセスすることができる、任意の情報のブロック又は情報を格納するためのリソースです。

Ruby on Rails

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

パスワード

パスワードは主に情報にアクセスする際に扱われます。主に、アクセス可能なユーザーを限定する手段として使われます。

HTML

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

0グッド

0クリップ

投稿2021/09/24 08:12

概要

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

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

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

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

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

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

guest

回答1

0

ベストアンサー

validates :password, format: { with: VALID_PASSWORD_REGEX }常に適用されてしまっています。

allow_blank: trueを追加して、パスワードを入力していない場合にはこのバリデーションをスルーさせるようにしましょう。

投稿2021/09/24 08:16

maisumakun

総合スコア145183

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

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

mokosamejima

2021/09/24 08:19

ご回答いただき、ありがとうございます! 無事解決することができました!
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問