@jum113

Are you sure you want to delete the question?

Leaving a resolved question undeleted may help others!

ユーザー編集機能を実装しようとしたが、updateメソッドが働いてくれない。

解決したいこと

Ruby on RailsとJavaScriptを用いてWebアプリをつくっています。
deviseでユーザー管理機能を実装したので、ユーザー編集機能も実装しようとしたが、編集フォームからupdateアクションには処理されるものの、updateメソッドが働いてくれない。エラーは起きていないので、原因が分からない。

発生している問題

下記の様に、updateメソッドが働かず、render: :editで編集ページにレンダリングされてしまう。

Started PATCH "/users/1" for ::1 at 2022-02-05 02:04:47 +0900
Processing by UsersController#update as HTML
Parameters: {"authenticity_token"=>"DLbzXY4JUc4j9X1XHPGxytTMzGs/oL77aERS5AaFMMP7EM76S44WJvz/jYhuD5Yq+QA+ddmZ1HUO0j9IUEqELg==", "user"=>{"nickname"=>"test1", "email"=>"t@t"}, "commit"=>"Update", "id"=>"1"}
User Load (0.8ms) SELECT `users`.* FROM `users` WHERE `users`.`id` = 1 ORDER BY `users`.`id` ASC LIMIT 1
Rendering users/edit.html.erb within layouts/application
Rendered users/edit.html.erb within layouts/application (Duration: 2.7ms | Allocations: 726)
[Webpacker] Everything's up-to-date. Nothing to do
Completed 200 OK in 27ms (Views: 18.6ms | ActiveRecord: 0.8ms | Allocations: 12297)

該当するソースコード

Rails.application.routes.draw do
  devise_for :users
  root 'notes#index'
  resources :notes, only: [:index, :create, :destroy] do
    collection do
      get 'search'
    end
  end
  resources :lists, only: [:create, :destroy]
  resources :users, only: [:edit, :update]
end
class UsersController < ApplicationController
  def edit
  end

  def update
    if current_user.update(user_params)
      redirect_to root_path
    else
      render :edit
    end
  end

  private

  def user_params
    params.require(:user).permit(:nickname, :email)
  end
end
<div class='account-page' id='account-page'>
  <div class='account-page__inner clearfix'>
    <div class='account-page__inner--left account-page__header'>
      <h2>Edit Account</h2>
      <h5>アカウントの編集</h5>
      <%= link_to "ログアウト", destroy_user_session_path, method: :delete, class: 'btn' %>
      <%= link_to "戻る", :back, class: 'btn' %>
    </div>
    <div class='account-page__inner--right user-form'>
      <%= form_with model: current_user, local: true do |f| %>
        <div class='field'>
          <div class='field-label'>
            <%= f.label :nickname %>
          </div>
          <div class='field-input'>
            <%= f.text_field :nickname, autofocus: true %>
          </div>
        </div>
        <div class='field'>
          <div class='field-label'>
            <%= f.label :email %>
          </div>
          <div class='field-input'>
            <%= f.email_field :email %>
          </div>
        </div>
        <div class='actions'>
          <%= f.submit "Update", class: 'btn' %>
        </div>
      <% end %>
    </div>
  </div>
</div>

自分で試したこと

class UsersController < ApplicationController
  def edit
  end

  def update
    binding.pry
    if current_user.update(user_params)
      redirect_to root_path
    else
      render :edit
    end
  end

  private

  def user_params
    params.require(:user).permit(:nickname, :email)
  end
end

binding.pryで下記結果のようにupdateアクションに処理が通っている事を確認した。

5: def update
=> 6: binding.pry
7: if current_user.update(user_params)
8: redirect_to root_path
9: else
10: render :edit
11: end
12: end

[1] pry(#)> params
=> "patch", "authenticity_token"=>"aysKPuQzG05ronRGaUofGrznio2INYhn5KryYdxJdzzxI47GDia1KaVbAFxxd9/bJwBZZOeePlmcU1sSDpBGXg==", "user"=>{"nickname"=>"test1", "email"=>"t@t"}, "commit"=>"Update", "controller"=>"users", "action"=>"update", "id"=>"1"} permitted: false>

また、updateに失敗してrender: :editが処理され、編集ページにレンダリングされているのも確認した。

0 likes

1Answer

すでに日が経っているので、解決済みですかね?

こちらの記事が参考にならないでしょうか?

Railsのモデルを更新、作成する際に発生したエラーに関しては、モデルのインスタンス(今回で言うとcurrent_userです。).errors.full_messagesに原因が記載されていることが多いので、エラーログの他にここを見れば良いと思います。

0Like

Comments

  1. @jum113

    Questioner

    回答有難うございます。一応解決できました。
    User.rb(モデル ユーザー)でパスワードに英数字混合のバリデーションを掛けていたのが原因でした。
    current_user.errors.full_messagesで上記のバリデーションに引っ掛かっていました。とりあえず、バリデーションを無しにしたら、更新できました。
  2. 解決おめでとうございます!
    モデルで登録できない系のエラーに関しては、エクセプションの他にerrors.full_messagesを見る癖をつけると良いかもですよ〜!
  3. @jum113

    Questioner

    有難うございます。
    errors.full_messagesは、テストではよく使っていたのですが、今回考えられませんでした。
    癖をつけることは大事ですね!
    心掛けます。

Your answer might help someone💌