0

我对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

在我的主题显示操作视图中,我有以下链接:

= 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" 怎么了?

4

2 回答 2

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
于 2013-10-16T08:21:01.333 回答
0

我会将 PostsController 代码更改为重定向到:

redirect_to :back

如果您使用的是 Rails 5,这会更好:

redirect_back(fallback_location: root_path)

于 2017-04-12T13:27:27.863 回答