public
Last active

How to create a poll System

  • Download Gist
_option
1 2 3 4 5
<div class="radio_btn_container">
<input type="radio" name="poll_option" value=<%= option.id %>>
<%= option.option %>
</input>
</div>
_poll.html.erb
HTML+ERB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
 
 
<%= render :partial => 'polls/poll_response', :locals =>{:poll => poll} %>
 
<div id="poll_errors"></div>
<div class="poll_option" id="poll_option_<%= poll.id %>">
<%= form_for(:poll, :remote => true, :url => poll_votes_path, :method =>:post,:loading =>"$('poll_option_#{poll.id}').hide(); $('spinner_#{poll.id}').show();",:complete =>"$('spinner_#{poll.id}').hide();$('poll_option_#{poll.id}').show();") do |f| %>
<%= render :partial => 'polls/option', :collection => poll.poll_options %>
<button class="buttons" type="submit">
<%= image_tag("vote.png") %>
</button>
<% end %>
</div>
<div id="spinner_<%= poll.id%>" style="display: none;"><%= image_tag('spinner.gif') %></div>
 
 
 
<span class="widget_lrg_btm"></span>
poll.rb #model
1 2 3 4 5 6 7 8 9 10
has_many :poll_options, :dependent => :destroy
validates_presence_of :question
validates_presence_of :user
has_many :poll_responses, :dependent => :destroy
belongs_to :user
 
 
accepts_nested_attributes_for :poll_options, :poll_responses
scope :public , :conditions => ["public = ?", true]
scope :open_polls , :conditions => ["status = ?", true]
poll_option.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13
class PollOption < ActiveRecord::Base
belongs_to :poll
validates_uniqueness_of :option, :scope => :poll_id, :message => 'has already.'
validates_length_of :option, :maximum => 70
has_many :poll_responses, :dependent => :destroy
 
 
def votes_percentage(precision = 1)
total_votes = poll.poll_responses.count
percentage = total_votes.eql?(0) ? 0 : ((poll_responses.count.to_f/total_votes.to_f)*100)
"%01.#{precision}f" % percentage
end
end
poll_responces.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
class PollResponse < ActiveRecord::Base
belongs_to :poll
belongs_to :user
belongs_to :poll_option
 
validates_presence_of :user
validates_presence_of :poll_option
validates_presence_of :poll_id
validates_uniqueness_of :user_id, :scope => :poll_id, :message => 'has already voted.'
 
after_save :update_poll_votes_count
 
def update_poll_votes_count
self.poll.update_attribute 'votes_count', self.poll.votes_count + 1
end
 
 
end
routes
1 2 3
resources :poll_votes
 
resources :polls
show.html.erb
HTML+ERB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
<% title "Poll" %>
 
<p>
<strong>Title:</strong>
<%= @poll.question %>
</p>
 
<p>
<strong>Votes Count:</strong>
<%= @poll.votes_count %>
</p>
 
 
<%= render :partial => 'poll', :locals =>{:poll => @poll} %>
 
 
 
 
<p>
<%= link_to "Edit", edit_poll_path(@poll) %> |
<%= link_to "Destroy", @poll, :confirm => 'Are you sure?', :method => :delete %> |
<%= link_to "View All", polls_path %>
</p>

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.