gpt4 book ai didi

ruby-on-rails - 评论投票与 acts as votable

转载 作者:行者123 更新时间:2023-12-04 06:26:00 26 4
gpt4 key购买 nike

我正在尝试使用 Acts as votable gem 对帖子中的评论实现投票系统。在这个阶段我遇到了这个错误

ActionController::UrlGenerationError in Posts#show 

接着是-

No route matches {:action=>"upvote", :controller=>"comments", :id=>nil, :post_id=>#<Comment id: 5, post_id: 3, body: "abc", created_at: "2014-01-12 20:18:00", updated_at: "2014-01-12 20:18:00", user_id: 1>, :format=>nil} missing required keys: [:id]. 

我的路线很弱。

我的路线.rb

resources :posts do
resources :comments do
member do
put "like", to: "comments#upvote"
put "dislike", to: "comments#downvote"
end
end
end

评论 Controller

def upvote
@post = Post.find(params[:post_id])
@comment = @post.comments.find(params[:id])
@comment.liked_by current_user
redirect_to @post
end

def downvote
@post = Post.find(params[:post_id])
@comment = @post.comments.find(params[:id])
@comment.downvote_from current_user
redirect_to @post
end

_comment.html.erb

<%= link_to "Upvote", like_post_comment_path(comment), method: :put %>
<%= link_to "Downvote", dislike_post_comment_path(comment), method: :put %>

最佳答案

您还应该在 like_post_comment_path 中传递帖子的 id,例如 like_post_comment_path(post, comment)

关于ruby-on-rails - 评论投票与 acts as votable,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21214558/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com