0

I have the Thumbs_Up gem loaded and the voting is working fine.

I added this code to the posts controller:

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

I just can't figure out what to put in the actual view to get it to display.

Is it just <% poll_winners %> ?

EDIT2: Here's the complete error message:

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

*EDIT*Here's my complete posts controller (not sure if it is right):

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
mystic cola
  • 1,465
  • 1
  • 21
  • 39

1 Answers1

1

You can just loop through the results of the method poll_winners

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

You can then get a specific attribute of Post, for example if it has a title you can just do <%= pw.title %> instead of just <%= pw %> which will return the object.

I am assuming that the method is as follows

def poll_winners
  @posts = Post.tally(
    :at_least => 1,      
    :limit => 20,
    :order => 'vote_count desc'
  })
end
Luís Ramalho
  • 10,018
  • 4
  • 52
  • 67
  • Now I'm getting "undefined local variable poll_winners" is this posts controller right? 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 – mystic cola Jun 30 '13 at 10:51
  • This is the complete message: undefined local variable or method `poll_winners' for #<#:0x007f55806c3360> – mystic cola Jun 30 '13 at 11:04
  • Please move the `poll_winners` method from the controller to the helper. In the helpers folder you should have a file called `post_helper.rb`. – Luís Ramalho Jun 30 '13 at 11:06