1

是否可以在没有用户登录的情况下使用 thumbs_up gem?基本上,我希望任何人都无需登录即可投票,例如http://www.yourather.com

最后,当我尝试对评论进行投票时,我收到一条路由错误消息,在对文章进行投票时也会发生同样的情况。

No route matches [GET] "/comments/1/vote_up"

模型:

class Comment < ActiveRecord::Base
  acts_as_voteable
end

class Article < ActiveRecord::Base
  acts_as_voteable
end

看法:

<%= link_to image_tag("up_arrow.jpeg"), vote_up_comment_path(@comment), 
    :method => 'post' %></br>
<%= link_to image_tag("down_arrow.jpeg"), vote_down_comment_path(@comment), 
    :method => 'post' %></br>

控制器:

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

def vote_down
  begin
    vote_against(@comment = Comment.find(params[:id]))
    render :nothing => true, :status => 200
  rescue ActiveRecord::RecordInvalid
    render :nothing => true, :status => 404
  end
end

路线.rb:

resources :comments do
  member do
    post :vote_up
    post :vote_down
  end
end

resources :articles do
  member do
    post :vote_up
    post :vote_down
  end
end

Rake Routes(相关部分):

vote_up_comment   POST    /comments/:id/vote_up(.:format)      comments#vote_up
vote_down_comment POST    /comments/:id/vote_down(.:format)    comments#vote_down
comments          GET     /comments(.:format)                  comments#index
                  POST    /comments(.:format)                  comments#create
new_comment       GET     /comments/new(.:format)              comments#new
edit_comment      GET     /comments/:id/edit(.:format)         comments#edit
comment           GET     /comments/:id(.:format)              comments#show
                  PUT     /comments/:id(.:format)              comments#update
                  DELETE  /comments/:id(.:format)              comments#destroy
vote_up_article   POST    /articles/:id/vote_up(.:format)      articles#vote_up
vote_down_article POST    /articles/:id/vote_down(.:format)    articles#vote_down
articles          GET     /articles(.:format)                  articles#index
                  POST    /articles(.:format)                  articles#create
new_article       GET     /articles/new(.:format)              articles#new
edit_article      GET     /articles/:id/edit(.:format)         articles#edit
article           GET     /articles/:id(.:format)              articles#show
                  PUT     /articles/:id(.:format)              articles#update
                  DELETE  /articles/:id(.:format)              articles#destroy
root                      /                                    voteables#index
                          /comment/:id(.:format)               comments#show
                          /article/:id(.:format)               articles#show

我已经查看了关于如何在 Rails 3和其他相关问题中使用“thumbs_up”投票宝石的澄清,但我无法使其工作。谢谢

4

1 回答 1

1

link_to 上的文档说 :method 选项应该是一个符号。尝试从以下位置更改视图:

<%= link_to image_tag("up_arrow.jpeg"), vote_up_comment_path(@comment), 
:method => 'post' %></br>

<%= link_to image_tag("up_arrow.jpeg"), vote_up_comment_path(@comment), 
:method => :post %></br>
于 2012-08-13T16:50:27.417 回答