前提・実現したいこと
ユーザー登録情報を変更した後に、変更内容をupdateさせたい。updateをしようとしてもrenderでeditページに戻ってしまう。
発生している問題・エラーメッセージ
Started PATCH "/users/3" for ::1 at 2020-09-20 16:15:02 +0900 Processing by UsersController#update as HTML Parameters: {"authenticity_token"=>"Qc88vYX/mt2UR6csI9ahU6M+S795ElzNGqB9ldSs8YU+GXzSSv7W08vsRJlvww5ZQwdsVd1z1KbJnLhmKK8ooQ==", "user"=>{"name"=>"Ns.jimin", "email"=>"a@a"}, "commit"=>"Change your account", "id"=>"3"} User Load (0.4ms) SELECT `users`.* FROM `users` WHERE `users`.`id` = 3 ORDER BY `users`.`id` ASC LIMIT 1 (0.2ms) BEGIN ↳ app/controllers/users_controller.rb:6:in `update' User Exists? (0.5ms) SELECT 1 AS one FROM `users` WHERE `users`.`name` = BINARY 'Ns.jimin' AND `users`.`id` != 3 LIMIT 1 ↳ app/controllers/users_controller.rb:6:in `update' (0.2ms) ROLLBACK ↳ app/controllers/users_controller.rb:6:in `update' Rendering users/edit.html.erb within layouts/application Rendered users/edit.html.erb within layouts/application (Duration: 1.0ms | Allocations: 445) [Webpacker] Everything's up-to-date. Nothing to do Completed 200 OK in 25ms (Views: 16.2ms | ActiveRecord: 1.2ms | Allocations: 22272)
該当のソースコード
ruby
1class UsersController < ApplicationController 2 def edit 3 end 4 5 def update 6 if current_user.update(user_params) 7 redirect_to root_path 8 else 9 render :edit 10 end 11 end 12 13 private 14 15 def user_params 16 params.require(:user).permit(:name, :email) 17 end 18end
ruby
1app/views/users/edit.html.erb 2<div class="login-view"> 3 <div class="login-form"> 4 <%= form_with model: current_user, url: user_path, local: true do |f|%> 5 <div class="login-text"> 6 <%= link_to "finish the work", destroy_user_session_path, method: :delete, class: 'login-submit'%> 7 <%= link_to "Back to the hospital", :back, class: 'login-submit'%> 8 </div> 9 <div class='input-field'> 10 <%= f.text_field :name, class:"login-username", autofocus: true, placeholder:"Name" %> 11 <%= f.email_field :email, class:"login-username", autofocus: true, placeholder:"Email" %> 12 </div> 13 <%= f.submit "Change your account", class: 'login-submit' %> 14 <% end %> 15 </div> 16 <div class="underlay-logout-photo"></div> 17 <div class="underlay-black"></div> 18</div>
ruby
1 Prefix Verb URI Pattern Controller#Action 2 new_user_session GET /users/sign_in(.:format) devise/sessions#new 3 user_session POST /users/sign_in(.:format) devise/sessions#create 4 destroy_user_session DELETE /users/sign_out(.:format) devise/sessions#destroy 5 new_user_password GET /users/password/new(.:format) devise/passwords#new 6 edit_user_password GET /users/password/edit(.:format) devise/passwords#edit 7 user_password PATCH /users/password(.:format) devise/passwords#update 8 PUT /users/password(.:format) devise/passwords#update 9 POST /users/password(.:format) devise/passwords#create 10 cancel_user_registration GET /users/cancel(.:format) devise/registrations#cancel 11 new_user_registration GET /users/sign_up(.:format) devise/registrations#new 12 edit_user_registration GET /users/edit(.:format) devise/registrations#edit 13 user_registration PATCH /users(.:format) devise/registrations#update 14 PUT /users(.:format) devise/registrations#update 15 DELETE /users(.:format) devise/registrations#destroy 16 POST /users(.:format) devise/registrations#create 17 patients_index GET /patients/index(.:format) patients#index 18 root GET / patients#index 19 edit_user GET /users/:id/edit(.:format) users#edit 20 user PATCH /users/:id(.:format) users#update 21 PUT /users/:id(.:format) users#update
ActiveRecord::RecordInvalid in UsersController#update Validation failed: Password
ruby
1class User < ApplicationRecord 2 devise :database_authenticatable, :registerable, 3 :recoverable, :rememberable, :validatable 4 5 has_many :patient_users 6 has_many :patients, through: :patient_users 7 8 with_options presence: true do 9 validates :name, uniqueness: { case_sensitive: true }, length: { maximum: 15 } 10 end 11 PASSWORD_REGEX = /\A(?=.*?[a-z])(?=.*?[\d])[a-z\d]+\z/i.freeze 12 validates_format_of :password, with: PASSWORD_REGEX, message: '' 13end
試したこと
binding.pryをコントローラーにかけて見るが、user_paramsは正常に送られていた。
補足情報(FW/ツールのバージョンなど)
rails6.0
初心者です。よろしくお願いいたします。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2020/09/20 13:38
2020/09/20 21:05
2020/09/21 10:53
2020/09/21 11:28
2020/09/21 18:48
2020/09/21 23:49