Ruby On Rails:赞成投票显示理货结果

时间:2013-06-29 20:17:29

标签: ruby-on-rails ruby-on-rails-3.2 voting voting-system vote-up-buttons

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

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

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

1 个答案:

答案 0 :(得分: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