ruby-on-railsruby-on-rails-3.2votingvoting-systemvote-up-buttons

Ruby On Rails: Thumbs Up Voting Display Tally Results


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

Solution

  • 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