0

我已经加载了 Thumbs_Up gem,并且投票工作正常。

我将此代码添加到帖子控制器:

def poll_winners
@posts = Post.tally(
{   :at_least => 1,      
  :limit => 20,
  :order => 'vote_count desc'
})

我只是不知道在实际视图中放置什么以使其显示。

只是<% poll_winners %>吗?

EDIT2:这是完整的错误消息:

undefined local variable or method `poll_winners' for #<#<Class:0x000000040a4278>:0x007f55806c3360>

*编辑*这是我完整的帖子控制器(不确定是否正确):

class PostsController < InheritedResources::Base
def vote_up
begin
  current_user.vote_for(@post = Post.find(params[:id]))
  redirect_to [@post]
  flash[:success] = "You have voted successfully"
rescue ActiveRecord::RecordInvalid
  redirect_to [@post]
  flash[:error] =  "You have already voted"
end
end
def poll_winners
  @posts = Post.tally(
{   :at_least => 1,
  :at_most => 10000,
  :limit => 10,
  :order => 'vote_count desc'
})
 end
end
4

1 回答 1

1

您可以遍历该方法的结果poll_winners

<% poll_winners.each do |pw| %>
  <%= pw %>
<% end %>

然后,您可以获得 的特定属性Post,例如,如果它具有title您可以执行的操作,<%= pw.title %>而不是仅<%= pw %>返回对象的操作。

我假设方法如下

def poll_winners
  @posts = Post.tally(
    :at_least => 1,      
    :limit => 20,
    :order => 'vote_count desc'
  })
end
于 2013-06-29T22:44:51.670 回答