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

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

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

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

Ruby on Rails

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

Q&A

解決済

1回答

1518閲覧

空欄でコメントをすると頻発してエラーが表示されてしまうので、解決したいです。

uoyuta

総合スコア7

Ruby

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

Ruby on Rails

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

0グッド

0クリップ

投稿2021/07/29 13:24

前提・実現したいこと

コメント機能に下記のようなシステムにしたいです。
しかし、空欄でコメントをすると頻発してエラーが表示されてしまうので、解決したいです。

【コメント機能に付与したいシステム】
コメント機能にて保存されたら、保存されたときは詳細ページにリダイレクト。
『空欄』でコメントを行い保存されなかったらrenderの記述で "prototypes/show"で詳細ページに戻りたい

※renderの記述は指定されております

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

NoMethodError in Comments#create Showing /Users/uot/projects/protospace-36401/app/views/prototypes/show.html.erb where line #35 raised: undefined method `to_model' for #<Comment::ActiveRecord_Associations_CollectionProxy:0x00007f8f90df3820> Did you mean? to_xml

該当するソースコード

show.html.erb

ruby

1<main class="main"> 2 <div class="inner"> 3 <div class="prototype__wrapper"> 4 <p class="prototype__hedding"> 5 <%= @prototype.title %> 6 </p> 7 <%= link_to "by #{@prototype.user.name}" , user_path(@prototype.user.id), class: :prototype__user %> 8 <% if user_signed_in? && current_user.id == @prototype.user_id %> 9 <div class="prototype__manage"> 10 <%= link_to "編集する", edit_prototype_path(@prototype.id), class: :prototype__btn %> 11 <%= link_to "削除する", prototype_path(@prototype.id) , method: :delete, class: :prototype__btn %> 12 </div> 13 <% end %> 14 <%# // プロトタイプの投稿者とログインしているユーザーが同じであれば上記を表示する %> 15 <div class="prototype__image"> 16 <%= image_tag @prototype.image %> 17 </div> 18 <div class="prototype__body"> 19 <div class="prototype__detail"> 20 <p class="detail__title">キャッチコピー</p> 21 <p class="detail__message"> 22 <%= @prototype.catch_copy %> 23 </p> 24 </div> 25 <div class="prototype__detail"> 26 <p class="detail__title">コンセプト</p> 27 <p class="detail__message"> 28 <%= @prototype.concept %> 29 </p> 30 </div> 31 </div> 32 <div class="prototype__comments"> 33 <%# ログインしているユーザーには以下のコメント投稿フォームを表示する %> 34 <% if user_signed_in? %> 35 <%= form_with model: [@prototype , @comment], local: true do |f| %> 36 <div class="field"> 37 <%= f.label :text, "コメント" %><br /> 38 <%= f.text_field :text, id:"comment_text" %> 39 </div> 40 <div class="actions"> 41 <%= f.submit "送信する", class: :form__btn %> 42 </div> 43 <% end %> 44 <% end %> 45 <ul class="comments_lists"> 46 <% @comments.each do |comment| %> 47 <li class="comments_list"> 48 <%= comment.text %> 49 <%= link_to comment.user.name, user_path(comment.user), class: :comment_user %> 50 </li> 51 <% end %> 52 </ul> 53 </div> 54 </div> 55 </div> 56</main> 57

comments_controller.rb

ruby

1class CommentsController < ApplicationController 2 def create 3 @comment = Comment.new(comment_params) 4 if @comment.save 5 redirect_to prototype_path (@comment.prototype) 6 else 7 @prototype = @comment.prototype 8 @comment = @prototype.comments 9 render "prototypes/show" 10 end 11end 12 13 private 14 def comment_params 15 params.require(:comment).permit(:text).merge(user_id: current_user.id, prototype_id: params[:prototype_id]) 16 end 17end 18

prototypes_controller.rb

ruby

1class PrototypesController < ApplicationController 2 before_action :authenticate_user!, only: [:new, :edit, :destroy] 3 before_action :move_to_index, only: :edit 4 def index 5 @prototypes =Prototype.all 6 7 end 8 9 def new 10 @prototype = Prototype.new 11 end 12 13 def create 14 @prototype = Prototype.new(prototype_params) 15 @prototypesave = Prototype.create(prototype_params) 16 if @prototypesave.save 17 redirect_to root_path 18 else 19 @prototype = Prototype.new(prototype_params) 20 render :new 21 end 22 end 23 24 def show 25 @prototype = Prototype.find(params[:id]) 26 @comment = Comment.new 27 @comments = @prototype.comments.includes(:user) 28 end 29 30 def edit 31 @prototype = Prototype.find(params[:id]) 32 33 end 34 35 def update 36 @prototype = Prototype.find(params[:id]) 37 if @prototype.update(prototype_params) 38 redirect_to prototype_path(prototype_params) 39 else 40 @prototype = Prototype.find(params[:id]) 41 render :edit 42 end 43 end 44 45 def destroy 46 prototype = Prototype.find(params[:id]) 47 if prototype.destroy 48 redirect_to root_path 49 end 50 end 51 52 def move_to_index 53 @prototype = Prototype.find(params[:id]) 54 unless current_user == @prototype.user 55 redirect_to root_path 56 end 57 end 58 59 private 60 def prototype_params 61 params.require(:prototype).permit(:title, :catch_copy, :concept, :image).merge(user_id: current_user.id) 62 end 63 64end 65

試したこと

未定義のメソッド to_model' があります。とのことでしたがto_modelはどこにも見当たらなかったです

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

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

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

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

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

guest

回答1

0

ベストアンサー

render先のshowページでは@commentsを定義する必要がありそうですが、それはできてるでしょうか?

インスタンス変数がもろもろ間違ってそうな気がするので、そのへんに原因があるのかと

投稿2021/07/29 15:02

kokitail

総合スコア135

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

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

uoyuta

2021/07/29 23:59

render先のshowページでは@comments で解決しました!なぜ@commentsにしたことで解決したのでしょうか?ご教示頂けますと幸いです。
kokitail

2021/07/30 01:56

renderとredirectの違いについて調べてみてください あと `@comment = @prototype.comments` ↑この記述ですが、変数名が単数なのに中身が複数になってしまうので、適切な変数名をつけるようにしたほうが良いです(他の人が読みにくく、バグの原因になりやすいので)
uoyuta

2021/07/30 07:49

ご回答頂きありがとうございました! 変数名に関しましても、指摘頂きありがとうございます!おかげで理解も深まりました
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.46%

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

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

質問する

関連した質問