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

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

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

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

Q&A

解決済

2回答

665閲覧

コメント投稿機能ActionController::UrlGenerationErrorについて

0W5E8fPq1EOm4yE

総合スコア13

Ruby on Rails

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

0グッド

1クリップ

投稿2020/11/28 03:50

編集2020/11/28 06:13

コメント機能を実装しているのですが投稿しようとしたところ以下の画像の通りにエラーが出てしまいました。
イメージ説明
解決したいこと
コメントを投稿できるようにしたい。

該当するソースコード

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

関連するソースコード

comments_controller.rb class CommentsController < ApplicationController def create @comment = Comment.new(comment_params) if @comment.save redirect_to prototype_path (@comment.prototype) else @prototype = @comment.prototype @comment = @prototype.comment render "prototypes/show" end end private def comment_params params.require(:comment).permit(:text).merge(user_id: current_user.id, prototype_id: params[:prototype_id]) end end
prototypes_controller.rb class PrototypesController < ApplicationController #CSRF保護を無効にする protect_from_forgery with: :null_session before_action :move_to_index, except: [:index, :show] before_action :authenticate_user!, only: [:index, :show] def index @prototype = Prototype.all end def new @prototype = Prototype.new end def create @prototype = Prototype.new(prototype_params) if @prototype.save redirect_to root_path (@prototype) else render :new @prototype = Prototype.includes(:user) end end def show @prototype = Prototype.find(params[:id]) @comment = Comment.new end def edit @prototype = Prototype.find(params[:id]) end def update @prototype = Prototype.find(params[:id]) @prototype.update(prototype_params) if @prototype.save redirect_to root_path(@prototype) else render :edit end end def destroy prototype = Prototype.find(params[:id]) prototype.destroy redirect_to root_path end private def prototype_params params[:prototype].permit(:title, :catch_copy, :concept, :image).merge(user_id: current_user.id) end def move_to_index unless user_signed_in? redirect_to action: :index end end def move_to_edit unless user_signed_in? redirect_to action: :index end end def move_to_update unless user_signed_in? redirect_to action: :index end end def move_to_destroy unless user_signed_in? redirect_to action: :index end end end

試したこと
commentコントローラーとprototypes/showに記述の誤りがないか確認した。

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

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

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

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

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

winterboum

2020/11/28 05:51

エラーメッセージを(全文)載せましょう
guest

回答2

0

自己解決

show.html.erbの
<%= f.label :concept, "コメント" %><br />
<%= f.text_field :concept %>

<%= f.label :text, "コメント" %><br />
<%= f.text_field :text %>
に直したところできました!

データベース上ではコメントはtextカラムでしたが以前の課題でconceptカラムの記述になったままなので保存されなかったそうです。

投稿2020/11/28 07:25

0W5E8fPq1EOm4yE

総合スコア13

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

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

0

prototype_id: params[:prototype_id] とありますが、このparams[:prototype_id] の値(今回4)が実在しないProtypeなのでしょう。
この4という値はどのようにして出てきた値ですか?

投稿2020/11/28 06:24

winterboum

総合スコア23347

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

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

0W5E8fPq1EOm4yE

2020/11/28 06:43

ありがとうございます! 4という値はどのようにして出てきた値ですか? →sequel Proのprototypesテーブルのidから出してます
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問