【问题标题】:No route matches {:action=>"vote_up", :controller=>"posts"} error for thumbs_up没有路线匹配 {:action=>"vote_up", :controller=>"posts"} thumbs_up 错误
【发布时间】:2012-05-24 04:08:53
【问题描述】:

我正在尝试将 thumbs_up gem 实现到我的应用程序中,即使我有用于 vote_up 操作的路线,我仍然会收到此错误

我的帖子控制器看起来像

   def vote_up
     begin
       current_user.vote_for(@post = Post.find(params[:id]))
       render :nothing => true, :status => 200
     rescue ActiveRecord::RecordInvalid
       render :nothing => true, :status => 404
     end
   end

我的路线看起来像

   resources :posts do
     get :vote_up, :on => :member
     resources :comments
   end

我不知道问题出在哪里,如果有人可以帮助我或向我指出使用 thumbs_up gem 的教程,那就太好了。

【问题讨论】:

  • 您能分享您的查看代码吗?
  • current_user.vote_for(@post = Post.find(params[:id]))。恕我直言,赋值也用作方法的参数不是好的编程习惯。

标签: ruby-on-rails ruby ruby-on-rails-3 voting voting-system


【解决方案1】:

问题是您请求的vote_up 没有帖子的ID。看看你的控制器:

current_user.vote_for(@post = Post.find(params[:id]))

但我在您的请求中没有看到任何 id 字段。问题在于你的看法。

【讨论】:

  • 同意。 vote_up 路由位于成员资源上。错误是因为您没有将 id 参数传递给此路由。
  • @Jordan Scales:修正后的代码应该是什么样的?你如何将id放入请求中?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-05-08
  • 2012-11-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-07-22
  • 2013-08-23
相关资源
最近更新 更多