前提・実現したいこと
deviseでのユーザー情報の編集時に、パスワード・確認用パスワードを空白のまま送信するとモデルのバリデーションに引っかかってしまい、編集することが出来ません。
デフォルトでは、空白のままの際も編集することができたように記憶しているため、出来なくなった原因がわかりません。
予想としてはモデルのバリデーションを自分で追加したためかなと思っています。
あとparans[:user][:password]に入っている[FILTERED]って何を意味してるのでしょうか?
教えていただけると幸いです。
発生している問題・エラーメッセージ
Started PUT "/users" for 106.73.176.128 at 2020-07-18 11:15:46 +0900 Cannot render console from 106.73.176.128! Allowed networks: 127.0.0.1, ::1, 127.0.0.0/127.255.255.255 Processing by Users::RegistrationsController#update as HTML Parameters: {"utf8"=>"✓", "authenticity_token"=>"AmDKx+82LWDCjOJOfazgc5zhXN7/FWEWO20NMZcoT4klC+bXfrF8GJg3+kQo+xdtzqjmCkVU/wYNSZ+E00MjgQ==", "user"=>{"image_cache"=>"", "name"=>"うんこ", "email"=>"a@email.com", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]", "current_password"=>"[FILTERED]"}, "commit"=>"プロフィール更新"} User Load (0.1ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? ORDER BY "users"."id" ASC LIMIT ? [["id", 105], ["LIMIT", 1]] ↳ /home/ec2-user/.rvm/gems/ruby-2.6.3/gems/activerecord-5.2.4.3/lib/active_record/log_subscriber.rb:98 User Load (0.1ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT ? [["id", 105], ["LIMIT", 1]] ↳ app/controllers/users/registrations_controller.rb:45 (0.1ms) begin transaction ↳ app/controllers/users/registrations_controller.rb:45 User Exists (0.2ms) SELECT 1 AS one FROM "users" WHERE "users"."name" = ? AND "users"."id" != ? LIMIT ? [["name", "うんこ"], ["id", 105], ["LIMIT", 1]] ↳ app/controllers/users/registrations_controller.rb:45 User Exists (0.2ms) SELECT 1 AS one FROM "users" WHERE LOWER("users"."email") = LOWER(?) AND "users"."id" != ? LIMIT ? [["email", "a@email.com"], ["id", 105], ["LIMIT", 1]] ↳ app/controllers/users/registrations_controller.rb:45 (0.0ms) rollback transaction ↳ app/controllers/users/registrations_controller.rb:45 Rendering users/registrations/edit.html.erb within layouts/application Rendered layouts/_error_messages.html.erb (0.8ms) Rendered users/registrations/edit.html.erb within layouts/application (5.3ms) Rendered layouts/_flash.html.erb (0.4ms) Completed 200 OK in 365ms (Views: 53.6ms | ActiveRecord: 0.7ms)
該当のソースコード
ruby
1class User < ApplicationRecord 2 # Include default devise modules. Others available are: 3 # :confirmable, :lockable, :timeoutable, :trackable and :omniauthable 4 #deviseの設定 5 devise :database_authenticatable, :registerable, 6 :recoverable, :rememberable 7 # :validatable 自分でバリデーションかけたくてコメントアウト 8 # バリデーション 9 10 VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+.[a-z]+\z/i 11 VALID_PASSWORD_REGEX = /\A(?=.*?[a-z])(?=.*?[A-Z])(?=.*?\d)[a-zA-Z\d]{8,100}+\z/ 12 13 validates :name, { presence: true, length: {maximum: 10}, uniqueness: true } 14 validates :email, { presence: true, uniqueness: { case_sensitive: false }, format: { with: VALID_EMAIL_REGEX } } 15 #半角英小文字大文字数字をそれぞれ1種類以上含む8文字以上100文字以下 16 validates :password, { presence: true, format: { with: VALID_PASSWORD_REGEX , 17 message: "半角英小文字、大文字、数字をそれぞれ1種類以上含む8文字以上100文字以下の必要があります" }, confirmation: true } 18end
edit.html.erb
html
1 <%= form_for(resource, as: resource_name, url: registration_path(resource_name), html: { method: :put }) do |f| %> 2 <%= render "layouts/error_messages", model: resource %> 3 4 <div class="form_group"> 5 <%= f.label :image, class: "image_label" do %> 6 <div class="prev-contents"> 7 <% if current_user.image.present? %> 8 <div class="prev-content"> 9 <%= image_tag current_user.image.url, size: '100x150', class: "prev-image rounded d-block mx-auto" %> 10 <% else %> 11 <i class="fa fa-frown-o fa-5x photo-icon"></i> 12 </div> 13 <% end %> 14 </div> 15 <% end %> 16 <%= f.file_field :image, class: "image_form__contents__field hidden_file form-control" %> 17 <%= f.hidden_field :image_cache %> 18 </div> 19 20 <div class="form_group"> 21 <label for="user_name">名前</label> <br /> 22 <%= f.text_field :name, autofocus: true, autocomplete: "name", class: "form-control" %> 23 </div> 24 25 <div class="form_group"> 26 <%= f.label :email %><br /> 27 <%= f.text_field :email, autocomplete: "email", class: "form-control" %> 28 </div> 29 30 <% if devise_mapping.confirmable? && resource.pending_reconfirmation? %> 31 <div>Currently waiting confirmation for: <%= resource.unconfirmed_email %></div> 32 <% end %> 33 34 <div class="form_group"> 35 <%= f.label :password %> <i>(leave blank if you don't want to change it)</i><br /> 36 <%= f.password_field :password, autocomplete: "new-password", class: "form-control" %> 37 <% if @minimum_password_length %> 38 <br /> 39 <em><%= @minimum_password_length %> characters minimum</em> 40 <% end %> 41 </div> 42 43 <div class="form_group"> 44 <%= f.label :password_confirmation %><br /> 45 <%= f.password_field :password_confirmation, autocomplete: "new-password", class: "form-control" %> 46 </div> 47 48 49 50 <div class="form_group"> 51 <%= f.label :current_password %> <i>(we need your current password to confirm your changes)</i><br /> 52 <%= f.text_field :current_password, autocomplete: "current-password", class: "form-control" %> 53 </div> 54 55 <div class="actions"> 56 <%= f.submit "プロフィール更新", class:"btn btn-primary mb-0" %> 57 </div> 58 <% end %>
補足情報(FW/ツールのバージョンなど)
ruby 2.6.3
Rails 5.2.4.3
win10
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。