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

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

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

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

Q&A

1回答

617閲覧

Rails6 ユーザー編集して更新がされない

Tayutar

総合スコア4

Ruby on Rails 6

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

0グッド

0クリップ

投稿2020/10/20 03:31

編集2020/10/20 06:49

前提・実現したいこと

ユーザー情報の更新
今までで更新できていたのですが、急にできなくなり、エラーも出ず、同じページに遷移します。
投稿の編集はしっかりできます。

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

エラーなし

該当のソースコード

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

試したこと

プロフィール画像とプロフィールを入力した後に更新ボタンをを押すと同じページに遷移するが、画像だけ消えてしまうので、nilになっているのか疑うが、全て入っている。
コントローラーまでの受け渡しとモデルにも記載しており、どこで渡っていないのかわからりません、、、

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

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

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

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

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

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

no1knows

2020/10/20 05:45

> エラーも出ていなくて跳ね返ってくる どういう意味でしょうか?多くの人が見るようなものは、適切な日本語で説明を心がけたほうが良いかと思います。 それはさておきデバッグするのが良いと思います。 if @user.update!(user_params)とupdateにビックリマークをつけてエラーを確認してみましょう。 https://qiita.com/ozin/items/5968971c9d2b3ab0a84d
Tayutar

2020/10/20 06:50 編集

申し訳ありませんおっしゃる通りです。 ご指摘ありがとうございます。 デバッグの末、ActiveRecord::RecordInvalid: Validation failed: Password is invalidと出ました。
guest

回答1

0

エラーが表示されないのは、表示するようにviewが書かれていないからです。

edit画面にpassword入力エリアがないため、validationに掛かっています。

password の validation に on: :create オプションつけてみてください

投稿2020/10/25 07:11

winterboum

総合スコア23401

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだベストアンサーが選ばれていません

会員登録して回答してみよう

アカウントをお持ちの方は

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

ただいまの回答率
85.46%

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

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

質問する

関連した質問