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

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

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

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

Q&A

解決済

1回答

1228閲覧

Rails6  ActiveRecord::RecordInvalid in UsersController#update

Tayutar

総合スコア4

Ruby on Rails 6

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

0グッド

0クリップ

投稿2020/10/20 09:02

編集2020/10/20 09:07

前提・実現したいこと

ユーザー情報の更新
ユーザー上の更新ができず、同じページに遷移します。
投稿の編集はしっかりできます。

発生している問題・エラーメッセージ

ActiveRecord::RecordInvalid in UsersController#update バリデーションに失敗しました: パスワードは不正な値です, パスワードを入力してください Extracted source (around line #21): def update 21 if @user.update!(user_params) redirect_to user_path(@user), notice: "ユーザー情報を更新しました。" else render :edit

該当のソースコード

users_controller.rb

class UsersController < ApplicationController before_action :set_user,only: [:show, :edit, :update] before_action :authenticate_user!, expect: [:index] def index @users = User.all end def show @username = current_user.username @posts = current_user.posts end def edit if @user != current_user redirect_to user_path,alert: '不正なアクセスです。' end end def update if @user.update(user_params) redirect_to user_path(@user), notice: "ユーザー情報を更新しました。" else render :edit end end def destroy reset_session redirect_to root_path, notice: 'ログアウトしました。' end private def set_user @user = User.find(params[:id]) end def user_params params.require(:user).permit(:username, :email, :profile, :profile_image) end end

users/edit.html.erb

<section class="hero is-dark"> <div class="hero-body"> <div class="container"> <h1 class="title"> ユーザー編集 </h1> </div> </div> </section> <section class="section"> <div class="container"> <div class="columns is-centered"> <div class="column is-6"> <%= form_for @user do |f| %> <div class="field"> <%= f.label :username, "ユーザー名", class: "label" %> <%= f.text_field :username, class: "input" %> </div> <div class="field"> <%= f.label :email, "メールアドレス", class: "label" %> <%= f.text_field :email, class: "input" %> </div> <div class="field"> <%= f.label :profile, "プロフィール", class: "label" %> <%= f.text_field :profile, class: "textarea" %> </div> <div class="field"> <%= f.label :profile_image, "プロフィール画像", class: "label" %> <%= f.attachment_field :profile_image, class: "input" %> </div> <%= f.submit "更新", class: "button is-success" %> <% end %> </div> </div> </div> </section>

users/show.html.erb

<section class="hero is-dark"> <div class="hero-body"> <div class="container"> <h1 class="title"> アウトプット詳細 </h1> </div> </div> </section> <section class="section"> <div class="container"> <div class="columns is-centered"> <div class="column is-7"> <div class="card"> <div class="card-content"> <div class="media"> <div class="media-content"> <p class="title is-4"><%= @post.title %></p> </div> </div> <div class="content"> <table class="table is-narrow"> <tr> <th>アクションプラン</th> </tr> <tr> <td><%= simple_format @post.body %></td> </tr> </table> <table class="table is-narrow-2"> <tr> <th>感想</th> </tr> <tr> <td><%= simple_format @post.impression %></td> </tr> </table> <% if @post.user.id == current_user.id %> <%= link_to "編集", edit_post_path(@post), class: "button is-success" %> <% end %> </div> </div> </div> </div> <div class="column is-4"> <article class="panel is-link"> <p class="panel-heading"> <%= @post.user.username %> </p> <div class="panel-block"> <p class="control"> <%= @post.user.profile %> </p> </div> <%= link_to user_path(@post.user), class: "panel-block" do %> <span class="panel-icon"> <i class="fas fa-user" aria-hidden="true"></i> </span> <%= @post.user.username %> さんのページへ <% end %> </article> </div> </div> </section> <div class="comment-wrapper border-top mb-10"> <p class="mt-5">コメント一覧</p> <% @comments.each do |c| %> <div> <% unless c.user.blank? %> <%= link_to user_path(c.user_id) %>"><%= image_tag c.user.image.to_s, class:"rounded-circle icon_image mr-3 mb-3"%> <% end %> <%= c.user.username unless c.user.blank? %> <br /> <%= c.content %> </div> <br /> <% end %> <% if user_signed_in? %> <%= form_with(model: [@post], local: true) do |f| %> <%= f.text_area :content, class: "form-control", rows: 5 %> <%= button_tag type: "submit", class: "btn btn-success float-right mt-1" do %> <i class="far fa-comments"></i> コメントする <% end %> <% end %> <% end %> </div>

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 validates :username, presence: true validates :password, format: {with: /\A(?=.*?[a-z])(?=.*?\d)[a-z\d]{6,100}+\z/i}, presence: true attachment :profile_image has_many :posts, dependent: :destroy has_many :comments end

試したこと

update!でエラーメッセージを確認し、マイグレーションファイルに無駄なパスワードのバリデーションがあったため、削除。

補足情報(FW/ツールのバージョンなど)

何時間もこのエラーで止まっており、思いつく仮説は全て試しましたが解決できませんでした。
どなたかお願い致します。

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

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

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

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

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

no1knows

2020/10/20 09:06

この質問と同じ内容ではないのでしょうか? せっかくデバッグしたのだからエラーを検索して調べればいいと思いますよ。 https://teratail.com/questions/299120
guest

回答1

0

ベストアンサー

validates :passwordを、allow_blank: trueとしてください。

パスワードはハッシュ化されて保存される関係上、テーブルから引いた時点ではpasswordとしてアクセスできる内容はになっています。Userの更新時に毎回パスワードを入力するのを義務付けるのでなければ、そのまま通す必要があります。

(なお、パスワードが本当に空になる事態はdevise側の設定で防ぐことが可能ですので、問題とはなりません)

投稿2020/10/20 09:09

maisumakun

総合スコア145203

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

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

Tayutar

2020/10/20 09:18

ありがとうございます。解決できました!! 助かりました。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.46%

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

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

質問する

関連した質問