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

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

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

Rubyはプログラミング言語のひとつで、オープンソース、オブジェクト指向のプログラミング開発に対応しています。

Ruby on Rails 6

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

Ruby on Rails

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

Q&A

解決済

1回答

1378閲覧

バリデーションに失敗し,テーブルに保存できない

pay_561

総合スコア26

Ruby

Rubyはプログラミング言語のひとつで、オープンソース、オブジェクト指向のプログラミング開発に対応しています。

Ruby on Rails 6

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

Ruby on Rails

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

0グッド

0クリップ

投稿2021/05/10 09:46

編集2021/05/10 13:34

##困っていること・前提
Railsで非同期通信でコメント機能を実装中ですがコメントしたコメントした一覧が表示されなく困っています。

イメージ説明
画像のようにROLLBACKされてしまいます。

参考にしたサイト
https://makunblog.com/programming/832/#i-7

https://qiita.com/yuto_1014/items/c7d6213139a48833e21a

https://ysk-pro.hatenablog.com/entry/2018/02/10/101739

##comments_controller.rb

class CommentsController < ApplicationController def create @post = Post.find(params[:post_id]) @comment = @post.comments.build(comment_params) @comment.user_id = current_user.id if @comment.save! render :index end end def destroy @comment = Comment.find(params[:id]) if @comment.destroy render :index end end private def comment_params params.require(:comment).permit(:content, :post_id, :user_id) end end

##アソシエーション設定
models/user.rb

class User < ApplicationRecord devise :database_authenticatable, :registerable, :recoverable, :rememberable, :validatable has_one_attached :image has_many :posts, dependent: :destroy validates :name, presence: true has_many :posts, dependent: :destroy has_many :comments, dependent: :destroy end

models/post.rb

class Post < ApplicationRecord has_many_attached :images has_one_attached :video belongs_to :user has_many :users belongs_to :user has_many :comments, dependent: :destroy end

models/comment.rb

class Comment < ApplicationRecord belongs_to :user belongs_to :item #バリデーション validates :content, presence: true end

##views/comments/_form.html.erb

<%= form_for [@post, @comment], remote: true do |f| %> <div class="field"> <div class="control"> <%= f.text_area :content, class:"input", placeholder:"コメント" %> <%= f.hidden_field :post_id, value: @post.id %> </div> </div> <br> <div class="field is-grouped"> <div class="control"> <%= f.submit 'コメントする', class:"button is-link form__submit" %> </div> </div> <% end %>

##views/comments/_index.erb

<% comments.each do |c| %> <div> <a href="/users/<%= c.user.id %>"><%= c.user.username %></a> <%= c.content %> <% if c.user.id == current_user.id %> <br> 投稿日:<%= c.created_at.to_s(:datetime_jp) %> <%= link_to post_comment_path(c.post_id, c.id), method: :delete, remote: true, data: {confirm: "削除しますか?"} do %> <span class="panel-icon"> <i class="fas fa-trash"></i> </span> <% end %> <hr> <% else %> <br> 投稿日:<%= c.created_at.to_s(:datetime_jp) %> <hr> <% end %> </div> <% end %>

##views/comments/index.js.erb

$("#comments_area").html("<%= j(render 'index', { comments: @comment }) %>") $("textarea").val('')

##追記

translation missing: ja.activerecord.errors.messages.record_invalid

と書かれていたのでGemfileにrails-i18nを追加しbundle installしたところこのようなエラーが発生しました。
イメージ説明

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

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

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

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

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

guest

回答1

0

自己解決

models/comment.rbをよく見ると

belongs_to :item

ではなく

belongs_to :post

でした。

それとモデルを作成する際にcontentカラムを作成したのですが

comment_contentカラムにという名前に変更しました。

投稿2021/05/11 02:45

pay_561

総合スコア26

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問