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

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

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

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

Q&A

1回答

722閲覧

ツイートへのコメントが保存出来ず表示されない

Estt

総合スコア1

Ruby

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

0グッド

0クリップ

投稿2020/12/04 17:00

Ruby初心者です。
プロトタイプへのコメント機能を実装したいのですが、コメントを保存しようとしたところ以下のエラーが表示されコメントが保存出来ません。
create!メソッドでバリデーションのエラーだとは分かったのですが、解決方法が分からず困っています。
またデータベースにもデータが保存されていないみたいで、アソシエーションが正しく組まれていないのかと考えているのですが‥
どなたか御助力お願い致します。

Rubyのエラー

Validation failed: Text can't be blank

show.html.erb

<main class="main"> <div class="inner"> <div class="prototype__wrapper"> <p class="prototype__hedding"> <%= @prototype.title %> </p> <%= link_to "by#{@prototype.user.name}", "/users/#{current_user.id}", class: :prototype__user %> <%# プロトタイプの投稿者とログインしているユーザーが同じであれば以下を表示する %> <% if user_signed_in? && current_user.id == @prototype.user_id %> <div class="prototype__manage"> <%= link_to "編集する", edit_prototype_path, class: :prototype__btn %> <%= link_to "削除する", prototype_path, class: :prototype__btn, method: :delete %> </div> <% end %> <%# // プロトタイプの投稿者とログインしているユーザーが同じであれば上記を表示する %> <div class="prototype__image"> <%= image_tag @prototype.image %> </div> <div class="prototype__body"> <div class="prototype__detail"> <p class="detail__title">キャッチコピー</p> <p class="detail__message"> <%= @prototype.catch_copy %> </p> </div> <div class="prototype__detail"> <p class="detail__title">コンセプト</p> <p class="detail__message"> <%= @prototype.concept %> </p> </div> </div> <div class="prototype__comments"> <%# ログインしているユーザーには以下のコメント投稿フォームを表示する %> <%= form_with(model: [@prototype, @comment], local: true) do |f|%> <div class="field"> <%= f.label :comment, "コメント" %><br /> <%= f.text_field :comment %> </div> <div class="actions"> <%= f.submit "送信する", class: :form__btn %> </div> <% end %> <%# // ログインしているユーザーには上記を表示する %> <ul class="comments_lists"> <%# 投稿に紐づくコメントを一覧する処理を記述する %> <li class="comments_list"> <%= @comment.text %> <%= link_to @comment.user, "/users/#{@comment.user_id}", class: :comment_user %> </li> <%# // 投稿に紐づくコメントを一覧する処理を記述する %> </ul> </div> </div> </div> </main>

routes.rb

Rails.application.routes.draw do devise_for :users root to: "prototypes#index" post 'prototypes/:id' => 'prototypes#show' resources :prototypes do resources :comments, only: [:create] end resources :users, only: :show end

comment.rb

class Comment < ApplicationRecord belongs_to :user belongs_to :prototype validates :text, presence: true end

prototype.rb

class Prototype < ApplicationRecord belongs_to :user has_many :comments, dependent: :destroy has_one_attached :image validates :title, presence: true validates :catch_copy, presence: true validates :concept, presence: true validates :image, presence: true end

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 :name, presence: true validates :profile, presence: true validates :occupation, presence: true validates :position, presence: true has_many :prototypes has_many :comments end

prototypes_controller.rb

class PrototypesController < ApplicationController before_action :authenticate_user! def index @prototypes = Prototype.all end def new @prototype = Prototype.new end def create if Prototype.create(prototype_params) redirect_to root_path else render :index end end def show @prototype = Prototype.find(params[:id]) @comment = Comment.new @comments = @prototype.comments.includes(:user) end def edit @prototype = Prototype.find(params[:id]) end def update prototype = Prototype.find(params[:id]) if prototype.update(prototype_params) redirect_to prototype_path else render :edit end end def destroy prototype = Prototype.find(params[:id]) prototype.destroy redirect_to root_path end private def prototype_params params.require(:prototype).permit(:title,:catch_copy,:concept,:image).merge(user_id: current_user.id) end end

comments_controller.rb

class CommentsController < ApplicationController before_action :authenticate_user! def create if comment = Comment.create!(comment_params) redirect_to "/prototypes/#{comment.prototype.id}" else render :index end end private def comment_params params.require(:comment).permit(:text).merge(user_id: current_user.id, prototype_id: params[:prototype_id]) end end

その他必要なファイル等あればなんなりとお申し付けください。
お手数おかけしますが、どうぞよろしくお願い致します。
環境
Rails 6.0.3.4
ruby 2.6.5p114

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

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

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

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

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

guest

回答1

0

エラーとコード見た感じ
バリデーションの定義では「text」という名称をチェックしようとしていますが、フォームには「text」という名称では設置されていないようです。

投稿2020/12/04 20:54

m.ts10806

総合スコア80854

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

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

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

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

ただいまの回答率
85.46%

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

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

質問する

関連した質問