Rubyで投稿内容にコメントできる機能を実装しています。
その実装中に以下のエラーメッセージが発生しました。
form_withの部分に問題があると思うのですが、どうして良いかわからず何時間も手が止まっています、、、。
解決策をご教授いただけますと幸いです。
Routing Error
No route matches [POST] "/prototypes/1"
実現したいこと
上記のエラーは、ある投稿内容に対するコメントを入力し、
保存ボタンを押した時に表示されました。
保存ボタンを押してもエラーが出ないようにしたいです。
該当のソースコード
以下は、app/views/prototypes/show.html.erbの該当箇所のコードです。
<%# ログインしているユーザーには以下のコメント投稿フォームを表示する %> <% if user_signed_in? %> <%= form_with (model: [@prototype, @comment], url: prototype_comments(@comment.prototype.id), method: :post, local: true) do |f|%> <div class="field"> <%= f.label :content, "コメント" %><br /> <%= f.text_field :content, id:"comment_content" %> </div> <div class="actions"> <%= f.submit "送信する", class: :form__btn %> </div> <% end %> <% end %>
以下はcommentsコントローラーのコードです。
class CommentsController < ApplicationController
def create
@comment = Comment.create(comment_params)
if @comment.save
redirect_to "/prototypes/#{@comment.ptorotype.id}"
else
render :"/prototypes/#{@comment.ptorotype.id}"
end
end
private
def comment_params
params.require(:comment).permit(:content).merge(user_id: current_user.id, prototype_id: params[:prototype_id])
end
end
試したこと
ここに問題に対して試したことを記載してください。
rails routesでルーティングのネスト(親がprototype、子がcomment)ができているか確認したところ、
下記URLの写真の通り、ルーティングはできていました。
https://gyazo.com/c8ef77debb535a01ca350354bb334195
補足情報(FW/ツールのバージョンなど)
prototypeとcommentは1対多の関係です。
ここにより詳細な情報を記載してください。
回答1件