是否可以在没有用户登录的情况下使用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
Routes.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
我查看了Clarification on how to use "thumbs_up" voting gem with Rails 3和其他相关问题,但我无法使其发挥作用。感谢
答案 0 :(得分:1)
docs on 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>