Ruby On Rails: Thumbs Up Voting Display Tally Results



我已经加载了Thumbs_Up宝石,投票工作正常。

我把这个代码添加到帖子控制器:

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

我只是不知道在实际视图中放什么来显示它。

它只是<% poll_winners %>吗?

编辑2:这是完整的错误消息:

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

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

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

您可以循环使用方法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

相关内容

  • 没有找到相关文章

最新更新