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

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

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

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

Q&A

解決済

1回答

1199閲覧

ユーザー情報が更新されない。

Misa0

総合スコア2

Ruby on Rails 6

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

0グッド

0クリップ

投稿2020/09/20 07:34

編集2020/09/21 10:52

前提・実現したいこと

ユーザー登録情報を変更した後に、変更内容を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

初心者です。よろしくお願いいたします。

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

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

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

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

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

guest

回答1

0

ベストアンサー

if current_user.update(user_params)

if current_user.update!(user_params)
にして試してください。エラーの原因が表示されます。
多分passwordがないというえらーだと予想されます。
そこ確認できたら、
モデルUserのvalidationの定義を載せてください

追記
validates_format_of :password に on: :create オプションを追加してください。
登録の時に必須で、それ以外は無くてもよい、とするため

投稿2020/09/20 11:55

編集2020/09/21 11:20
winterboum

総合スコア23347

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

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

Misa0

2020/09/20 13:38

ご回答有り難うございました! 仰られた通り、パスワードエラーが出ました! ``` ActiveRecord::RecordInvalid in UsersController#update Validation failed: Password ``` ```ruby class User < ApplicationRecord devise :database_authenticatable, :registerable, :recoverable, :rememberable, :validatable has_many :patient_users has_many :patients, through: :patient_users with_options presence: true do validates :name, uniqueness: { case_sensitive: true }, length: { maximum: 15 } end PASSWORD_REGEX = /\A(?=.*?[a-z])(?=.*?[\d])[a-z\d]+\z/i.freeze validates_format_of :password, with: PASSWORD_REGEX, message: '' end ``` バリデーションが悪さしてたんですかね。パスワードのバリデーションのかけ方がおかしいのでしょうか?
winterboum

2020/09/20 21:05

プログラムを質問欄に<code>で書いてください。コメント欄だとベタなので読みにくい
Misa0

2020/09/21 10:53

失礼しました。 質問欄に追記しましたので、よろしくお願い致します。
Misa0

2020/09/21 11:28

無事update出来ました!ありがとうございます! update時にはパスワードの入力は不要なため、with_options presence: trueはパスワードにつけていなかったのですが、何故反応してしまったのでしょうか?
winterboum

2020/09/21 18:48

validates_format_of :password が動いたのです。 空なのでformatにマッチしなかったのでエラー
Misa0

2020/09/21 23:49

ご回答有り難うございます! なるほど、format_ofオプション時にはallow_nilやallow_blankなどをかけない限りonオプションでバリデーションのタイミングをしてして置く事が必要そうですね! 有り難うございました!
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問