前提・実現したいこと
railsで投稿アプリを作っています。投稿した記事に対してコメント機能を実装しようと思い、form_withのsubmitを使い送信しようとしましたら**No route matches [POST] "/ideas/2"**というルーティングエラーが出てしまいました。
commentモデルはideaモデルにネストさせているため<%= form_with model: [@idea,@comment], local: true do |f| %>にしてみたり、url: idea_comments_pathとurlを指定してみても違うエラーが出てしまいます。
なぜ、submitできないか原因が分かる方いらっしゃいましたらご教示いただけますと幸いです。
足りない情報がありましたら恐縮ですが、お知らせいただけますと幸いです。
railsの学習を初めて1ヶ月半ほどなのですがいまだに理解度が足りないようです。。
補足:ブロードキャストを用いてメッセージを送信したとき、即時に更新されるようにしようと考えています。
該当のソースコード
###エラー文の画像
https://gyazo.com/6ee4c1f910658b903b2bd004a0effb6f
###_comment.html.erb
<%= form_with model: @comment, local: true do |f| %> <%= f.text_field :message %> <%= f.submit '送信'%> <% end %> <div id='comments'> <% if @comments %> <% @comments.each do |comment| %> <p><%= comment.message %></p> <% end %> <% end %> </div>
###routes.rb
Rails.application.routes.draw do devise_for :users root to: "ideas#index" resources :ideas do resources :likes, only: [:create, :destroy] resources :comments, only: [:new, :create] end end
###comments_controller.rb
class CommentsController < ApplicationController def new @comments = Comment.all @comment = Comment.new end def create @comment = Message.new(comment_params) if @comment.save ActionCable.server.broadcast 'comment_channel', content: @comment end end private def comment_params params.require(:comment).permit(:message).merge(user_id: current_user.id, idea_id: params[:idea_id]) end end
###show.html.erb
<header> <div class="header-inner"> <h1>アプリ名</h1> <nav class="header-nav"> <ul class="header-nav-list"> <% if user_signed_in? && @idea.user_id == current_user.id %> <li><%= link_to '編集', edit_idea_path, class: "header-nav-list-item" %></li> <li><%= link_to '削除', idea_path, method: :delete, class: "header-nav-list-item", data: { confirm: '削除しますか?' } %></li> <% end %> </ul> </nav> </div> </header> <div class="show"> <div class="idea-show"> <div class="idea-title"> <h1 class="title"> <%= @idea.title %> </h1> </div> <div class="idea-img"> <%= image_tag @idea.image, class: "idea-image" if @idea.image.attached? %> </div> <div class="idea-content"> <%= @idea.content %> </div> <%= render 'shared/like', locals: { idea: @idea } %> <%= render 'shared/comment', locals: {idea: @idea } %> </div> </div>
###channels/comment_channel.rb
class CommentChannel < ApplicationCable::Channel def subscribed stream_from "comment_channel" end def unsubscribed # Any cleanup needed when channel is unsubscribed end end
comment_channel.js
import consumer from "./consumer" consumer.subscriptions.create("CommentChannel", { connected() { // Called when the subscription is ready for use on the server }, disconnected() { // Called when the subscription has been terminated by the server }, received(data) { const html = `<p>${data.content.message}</p>`; const comments = document.getElementById('comments'); const newComment = document.getElementById('message_text'); comments.insertAdjacentHTML('afterbegin', html); newComment.value=''; } });
###model/comment.rb
class Comment < ApplicationRecord belongs_to :idea belongs_to :user end
###model/idea.rb
class Idea < ApplicationRecord belongs_to :user has_many :comments end
###model/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 has_many :ideas has_many :comments end
###path
Prefix Verb URI Pattern Controller#Action new_user_session GET /users/sign_in(.:format) devise/sessions#new user_session POST /users/sign_in(.:format) devise/sessions#create destroy_user_session DELETE /users/sign_out(.:format) devise/sessions#destroy new_user_password GET /users/password/new(.:format) devise/passwords#new edit_user_password GET /users/password/edit(.:format) devise/passwords#edit user_password PATCH /users/password(.:format) devise/passwords#update PUT /users/password(.:format) devise/passwords#update POST /users/password(.:format) devise/passwords#create cancel_user_registration GET /users/cancel(.:format) devise/registrations#cancel new_user_registration GET /users/sign_up(.:format) devise/registrations#new edit_user_registration GET /users/edit(.:format) devise/registrations#edit user_registration PATCH /users(.:format) devise/registrations#update PUT /users(.:format) devise/registrations#update DELETE /users(.:format) devise/registrations#destroy POST /users(.:format) devise/registrations#create root GET / ideas#index idea_likes POST /ideas/:idea_id/likes(.:format) likes#create idea_like DELETE /ideas/:idea_id/likes/:id(.:format) likes#destroy **idea_comments** POST /ideas/:idea_id/comments(.:format) comments#create ideas GET /ideas(.:format) ideas#index POST /ideas(.:format) ideas#create new_idea GET /ideas/new(.:format) ideas#new edit_idea GET /ideas/:id/edit(.:format) ideas#edit idea GET /ideas/:id(.:format) ideas#show PATCH /ideas/:id(.:format) ideas#update PUT /ideas/:id(.:format) ideas#update DELETE /ideas/:id(.:format) ideas#destroy
回答1件
あなたの回答
tips
プレビュー