0

我寻求澄清在 rails 4 中使用竖起大拇指 gem。我目前有一个用户资源和一个帖子资源,并按如下方式设置竖起大拇指。

将 gem 添加到 gemfile 并使用 bundler 安装它。生成需要迁移

用户模型

class User < ActiveRecord::Base
 acts_as_voter
end

后模型

class Post < ActiveRecord::Base
 acts_as_voteable
end

后控制器

def vote_up
 @post = Post.find(params[:id])
 current_user.vote_for(@post)
 respond_to do |format|
   format.js
 end
end

看法

<%= link_to('vote for this post!', vote_up_post_path(@post) , :method => :post) %>

路由文件

resources :posts do
  member do
   post :vote_up
  end
 end

但是我不断收到此错误

No route matches [POST] "/posts/vote_up"

在运行 rake 路线后,我可以看到以下路线可供我使用:

vote_up_post POST   /posts/:id/vote_up(.:format) posts#vote_up

任何想法可能是导致此错误的原因?

4

4 回答 4

0

“vote_up”就像一个 RESTful 动作,类似于“post/1/delete”、“post/1/edit”。所以你需要在路由中添加这个自定义的 RESTful 动作。

首先像这样改变你的路线。

resources :posts do
  member do
    post 'vote_up'
  end
end

然后,在您看来,要使用此路径,请将资源添加为 arg

vote_up_post_path @post

参考: http: //guides.rubyonrails.org/routing.html#adding-more-restful-actions

于 2013-08-25T12:22:58.713 回答
0

不确定,但在您看来,尝试将@post.id 赋予vote_up_post_path 而不是@post。

<%= link_to('vote for this post!', vote_up_post_path(@post.id) , :method => :post) %>
于 2014-05-28T10:10:13.660 回答
0

阅读这篇笔记的其他人应该意识到 post 不是他的控制器帖子的单数……而是 post http 动词

resources :contenders do
  member do
   post 'vote_up'
  end
end
于 2014-05-28T08:36:24.400 回答
0

您能否向我们展示您的观点。

显然,你在打电话

/posts/vote_up

代替

/posts/:id/vote_up
于 2013-08-25T12:11:11.527 回答