Created
June 25, 2011 23:42
-
-
Save ramza1/1047041 to your computer and use it in GitHub Desktop.
How to create a poll System
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<div class="radio_btn_container"> | |
<input type="radio" name="poll_option" value=<%= option.id %>> | |
<%= option.option %> | |
</input> | |
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<%= 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> | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
resources :poll_votes | |
resources :polls |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<% 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> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment