Skip to content

@ramza1 /_option
Created

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
How to create a poll System
<div class="radio_btn_container">
<input type="radio" name="poll_option" value=<%= option.id %>>
<%= option.option %>
</input>
</div>
<%= 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>
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]
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
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
resources :poll_votes
resources :polls
<% 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
Something went wrong with that request. Please try again.