我试图添加"喜欢" to" comments",以便用户能够喜欢评论。当我点击"喜欢"我在这一行上收到一条错误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
答案 0 :(得分:1)
这有点不常见:
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
此外,您无法在:discussion_id
内使用LikesController
更好的是:
POST /comments/:id/likes(.:format) likes#create
资源不应该嵌套超过1级。
resources :discussions do
resources :comments
end
resources :comments do
resources :likes, only: [:create, :destroy]
end
你也可以
comment_likes_path(item)
答案 1 :(得分:0)
在(评论)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 %>
答案 2 :(得分:0)
我认为问题在于您没有引用传递给您喜欢的控制器的参数。
您可以尝试通过更改:
将评论作为哈希传递给您的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]