前提・実現したいこと
rails6でSNSをせいさくしていて、投稿(book)に対するコメントの削除機能をつけようとしているのですが、コメントを削除するbook_comment_pathにコメントのidを渡すことができずに困っています。
発生している問題・エラーメッセージ
No route matches {:action=>"destroy", :book_id=>"315", :controller=>"comments", :id=>nil}, missing required keys: [:id]
該当のソースコード
routes.rb
Rails.application.routes.draw do root 'static_pages#home' get '/home', to: 'static_pages#home' get '/help', to: 'static_pages#help' get '/signup', to: 'users#new' get '/login', to: 'sessions#new' post '/login', to: 'sessions#create' delete '/logout', to: 'sessions#destroy' resources :users do member do get :following, :followers end end resources :books, only: [:show, :create, :destroy] do resources :likes, only: [:create, :destroy] resources :comments, only: [:create, :destroy] end resources :relationships, only: [:create, :destroy] end
booksコントローラー booksのshowページにコメントの削除機能があります。
def show @book = Book.find(params[:id]) @like = Like.find_by(book_id: @book.id, user_id: current_user.id) || Like.new @comments = @book.comments @comment = @book.comments.build end
commentsコントローラー
class CommentsController < ApplicationController def create # @comment = current_user.comments.new(comment_params @book = Book.find(params[:book_id]) @comment = current_user.comments.new(comment_params.merge(user_id: params[:user_id], book_id: params[:book_id])) if @comment.save flash[:success] = "成功" redirect_back(fallback_location: root_path) else flash[:danger] = "失敗" redirect_back(fallback_location: root_path) end end def destroy @comment.destroy flash[:success] = "コメントを削除しました" redirect_back(fallback_location: root_path) end private def comment_params params.require(:comment).permit(:content, :book_id, :user_id) end end
books/show.html.erb
<% provide(:title, @book.title) %> <div class="row"> <div class="col-md-6"> <div class="book_information"> <h2>『<%= @book.title %>』</h2> <p>著者: <%= @book.author %></p> <p>出版社: <%= @book.publisher %></p> <p>出版年: <%= @book.publish_year %>年</p> <p class="book_explanation"><%= @book.explanation %></p> <div class="liked_users"> <div class="iine"> <div id="like_form"> <% if current_user.already_liked?(@book) %> <%= render 'likes/unlike' %> <% else %> <%= render 'likes/like' %> <% end %> </div> <p> いいね <strong id="likes_count"> <%= @book.likes.count %> </strong> 件 </p> </div> <div id="liked_by"> <%= render 'liked_users' %> </div> </div> </div> </div> <div class="col-md-6"> <%= render 'comments/comment_form.html.erb' %> <%= render 'comments/show_comments.html.erb' %>#このパーシャルにコメントの削除フォームがあります。 </div> </div>
_show_comments.html.erb
<% if @comments.count == 0 %> <p>コメントはまだありません</p> <% else %> <div> <p>コメント<%= @comments.count %>件</p> <% @comments.each do |comment| %> <p><%= comment.content %></p> <span class="comment_delete"> <%= link_to "削除", **book_comment_path(@book, comment)**, method: :delete %> </span> <% end %> </div> <% end %>
commentモデル
class Comment < ApplicationRecord belongs_to :user belongs_to :book validates :content, presence: true, length: { maximum: 30 } end
bookモデル
class Book < ApplicationRecord belongs_to :user has_many :likes, dependent: :destroy has_many :liked_user, through: :likes, source: :user has_many :comments, dependent: :destroy has_many :commented_user, through: :comments, source: :user default_scope -> { order(created_at: :desc) } validates :title, presence: true, length: { maximum: 100 } validates :author, presence: true, length: { maximum: 50 } validates :publisher, presence: true, length: { maximum: 50 } validates :publish_year, presence: true validates :user_id, presence: true validates :explanation, length: { maximum: 140 } end
userモデル
class User < ApplicationRecord has_many :books, dependent: :destroy has_many :active_relationships, class_name: "Relationship", foreign_key: "follower_id", dependent: :destroy has_many :passive_relationships, class_name: "Relationship", foreign_key: "followed_id", dependent: :destroy has_many :following, through: :active_relationships, source: :followed has_many :followers, through: :passive_relationships, source: :follower has_many :likes, dependent: :destroy has_many :liked_books, through: :likes, source: :book has_many :comments, dependent: :destroy has_many :commented_books, through: :comments, source: :book before_save { self.email.downcase! } validates :name, presence: true, length: { maximum: 50 } VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-]+(.[a-z\d\-]+)*.[a-z]+\z/i validates :email, presence: true, length: { maximum: 255 }, format: { with: VALID_EMAIL_REGEX }, uniqueness: true has_secure_password validates :password, presence: true, length: { minimum: 6 }, allow_nil: true #渡された文字列のハッシュ値を返す def User.digest(string) cost = ActiveModel::SecurePassword.min_cost ? BCrypt::Engine::MIN_COST : BCrypt::Engine.cost BCrypt::Password.create(string, cost: cost) end def feed following_ids = "SELECT followed_id FROM relationships WHERE follower_id = :user_id" Book.where("user_id IN (#{following_ids}) OR user_id = :user_id", user_id: id) end def follow(other_user) following << other_user end def unfollow(other_user) active_relationships.find_by(followed_id: other_user.id).destroy end def following?(other_user) following.include?(other_user) end def followed_by?(other_user) followers.include?(other_user) end def already_liked?(book) self.likes.exists?(book_id: book.id) end end
あなたの回答
tips
プレビュー