Rails:找不到 'id'= 的评论

Rails: Couldn't find Comment with 'id'=

我正在尝试将 "likes" 添加到 "comments",以便用户可以点赞评论。当我单击 "like" 时,我收到一条错误消息,在此处的这一行显示 Couldn't find Comment with 'id'=comment = Comment.find params[:comment_id],它位于我的 likes_controller.rb#create 中。

likes_controller.rb

class LikesController < ApplicationController

  def create
    comment  = Comment.find params[:comment_id]
    like      = comment.likes.new
    like.user = current_user
    if like.save
      redirect_to new_discussion_comment_path, notice: "Liked!"
    else
      redirect_to new_discussion_comment_path, alert: "Liked Already!"
    end
  end

  def destroy
    comment = Comment.find params[:comment_id]
    like    = comment.likes.find params[:id]
    if like.destroy
      redirect_to new_discussion_comment_path, notice: "UnLiked!"
    else
      redirect_to new_discussion_comment_path, alert: "Can't UnLike!"
    end
  end
end

comments_controller.rb

class CommentsController < ApplicationController

  def new
    @discussion = Discussion.find(params[:discussion_id])
    @comment = Comment.new
  end

  def create
    @discussion = Discussion.find(params[:discussion_id])
    @comment = @discussion.comments.build(comment_params)
    if @comment.save
      redirect_to new_discussion_comment_path(@discussion)
    end
  end



  def edit
    @discussion = Discussion.find(params[:discussion_id])
    @comment = @discussion.comments.find(params[:id])
  end

  def update 
    @discussion = Discussion.find(params[:discussion_id])
    @comment = @discussion.comments.find(params[:id])
    if @comment.update_attributes(comment_params)
      redirect_to new_discussion_comment_path
    else
      render "edit"
    end
  end

  def destroy
    @comment = Comment.find(params[:id])
    @comment.destroy
    redirect_to new_discussion_comment_path
  end

  private
  def comment_params
    params.require(:comment).permit(:id, :description)
  end

end

(评论)_form.html.erb

<%= form_for [@discussion, @comment] do |f| %>
  <div class="container">
    <div class="form-group">
      <%= f.label :description %>
      <%= f.text_area :description, class: "form-control" %>
    </div>

    <div class="form-group">
      <%= f.submit "Submit comment", class: "btn btn-primary" %>
    </div>
  </div>
<% end %>

(评论)edit.html.erb

<%= render "form" %>

(评论)new.html.erb

<div class="container">
  <div class="page-header">
    <h1>Comments<small> Create a comment.</small></h1>
  </div>
</div>

Discussion: <%= @discussion.title %> <%= link_to "Go back?", projects_path %>


<%= render "form" %>


<% if !@discussion.project.blank? %>
  <% for item in @discussion.comments %>
  <div class="container">
  <div class="panel panel-default">
  <div class="panel-heading">
    <%= item.description %>
    <p>
      <%= link_to "Edit", edit_discussion_comment_path(@discussion, item) %> 
    </p>
     <% if user_signed_in? && current_user.has_liked?(item) %>
     <% like = current_user.likes.find_by_comment_id(item.id) %>
      <%= link_to "unlike", discussion_like_path(:discussion_id, item), class: "btn btn-info like  ", method: :delete %>
  <% else %>
      <%= link_to "like", discussion_likes_path(:discussion_id, item), class: "btn btn-info like ", method: :post %>
  <% end %>
  </div>
  </div>
  </div>
  <% end %>
<% end %>

路线:

discussion_comments GET    /discussions/:discussion_id/comments(.:format)          comments#index
                         POST   /discussions/:discussion_id/comments(.:format)          comments#create
  new_discussion_comment GET    /discussions/:discussion_id/comments/new(.:format)      comments#new
 edit_discussion_comment GET    /discussions/:discussion_id/comments/:id/edit(.:format) comments#edit
      discussion_comment GET    /discussions/:discussion_id/comments/:id(.:format)      comments#show
                         PATCH  /discussions/:discussion_id/comments/:id(.:format)      comments#update
                         PUT    /discussions/:discussion_id/comments/:id(.:format)      comments#update
                         DELETE /discussions/:discussion_id/comments/:id(.:format)      comments#destroy
        discussion_likes GET    /discussions/:discussion_id/likes(.:format)             likes#index
                         POST   /discussions/:discussion_id/likes(.:format)             likes#create
     new_discussion_like GET    /discussions/:discussion_id/likes/new(.:format)         likes#new
    edit_discussion_like GET    /discussions/:discussion_id/likes/:id/edit(.:format)    likes#edit
         discussion_like GET    /discussions/:discussion_id/likes/:id(.:format)         likes#show
                         PATCH  /discussions/:discussion_id/likes/:id(.:format)         likes#update
                         PUT    /discussions/:discussion_id/likes/:id(.:format)         likes#update
                         DELETE /discussions/:discussion_id/likes/:id(.:format)         likes#destroy

(评论)new.html.erb中,更改:

<%= link_to "like", discussion_likes_path(:discussion_id, item), class: "btn btn-info like ", method: :post %>

<%= link_to "like", discussion_likes_path(:discussion_id, item.id), class: "btn btn-info like ", method: :post %>

我认为问题在于您没有引用传递给点赞控制器的参数。

您可以尝试将您的评论作为散列传递给您的 likes_controller.rb,方法是更改​​:

<%= link_to "like", discussion_likes_path(:discussion_id, item), class: "btn btn-info like ", method: :post %>

<%= link_to "like", discussion_likes_path(:discussion_id, :comment_id => item.id), class: "btn btn-info like ", method: :post %>

在您的(评论)new.html.erb 中并在您的 likes_controller.rb 中引用此新参数:

comment = Comment.find params[:comment_id]

这有点不常见:

POST /discussions/:discussion_id/likes(.:format) likes#create

如您所见,没有 :comment_id,您可以将其作为查询参数传递,但这仍然不是执行此操作的标准方法。


这是一种更标准的方法:

POST /discussions/:discussion_id/comments/:comment_id/likes(.:format) likes#create

然后你可以:

 discussion_comment_likes_path(@discussion, item)

但这也不是很好:

 resources :discussions do
   resources :comments do
     resources :likes, only: [:create, :destroy]
   end
 end

此外,无论如何您都不会在 LikesController 中使用 :discussion_id


更好的是:

POST /comments/:id/likes(.:format) likes#create

because

Resources should never be nested more than 1 level deep.

resources :discussions do
  resources :comments
end

resources :comments do
  resources :likes, only: [:create, :destroy]
end

你可能

comment_likes_path(item)