我对acts_as_votable gem有疑问。我有简单的论坛应用程序,我希望有功能投票上下帖。我用过这个acts_as_votable gem。我已经向帖子控制器添加了两种方法:
def upvote
@post = post.find(params[:id])
@post.liked_by current_user
redirect_to forum_topic_path(@post.topic.forum, @post.topic)
end
def downvote
@post = post.find(params[:id])
@post.downvote_from current_user
redirect_to forum_topic_path(@post.topic.forum, @post.topic)
end
我的路线是:
resources :topics, except: :index do
resources :posts do
member do
put "like", to: "posts#upvote"
put "dislike", to: "posts#downvote"
end
end
end
在我的主题show action view中,我有以下链接:
= link_to "Upvote", like_topic_post_path(post.topic.id,post.id, method: :put)
= link_to "Downvote", dislike_topic_post_path(post.topic.id,post.id, method: :put)
当我尝试点击upvote或downvote时,我被重定向到:
http://localhost:3000/topics/104/posts/55/like?method=put
,我有以下错误:
没有路线匹配[GET]“/ topics / 104 / posts / 55 / like”
怎么了?
答案 0 :(得分:0)
尝试“GET”方法而不是“PUT”,如下所示,
resources :topics, except: :index do
resources :posts do
member do
get "like", to: "posts#upvote"
get "dislike", to: "posts#downvote"
end
end
end
答案 1 :(得分:0)
我将PostsController代码更改为重定向到:
redirect_to :back
如果您使用的是Rails 5,那就更好了:
redirect_back(fallback_location: root_path)