Class PollsController
In: app/controllers/polls_controller.rb
Parent: ApplicationController

Methods

index   results   results2   show   show2   vote  

Public Instance methods

Lista de todas las encuestas.

[Source]

    # File app/controllers/polls_controller.rb, line 10
10:   def index
11:     @polls = Poll.published.paginate :page => params[:page], :per_page => 15
12:   end

Mostar los resultados hasta el momento

[Source]

    # File app/controllers/polls_controller.rb, line 93
93:   def results
94:     @parent = @poll
95:     @comments = @poll.comments.approved.paginate(:page => params[:page] || 1, :per_page => 50)
96:   end

REPLACE results

[Source]

     # File app/controllers/polls_controller.rb, line 99
 99:   def results2
100:     @title.downcase!
101:     @parent = @poll
102:     @comments = @poll.comments.approved.paginate(:page => params[:page] || 1, :per_page => 50)
103:   end

Página de una encuesta

[Source]

    # File app/controllers/polls_controller.rb, line 15
15:   def show    
16:     @parent = @poll
17:     @comments = @poll.comments.approved.paginate(:page => params[:page] || 1, :per_page => 50)
18:     if can_edit?("polls") && !user_has_voted_poll?(@poll, current_user)
19:       if @poll.outdated?
20:         render :action => 'results' and return
21:       end
22:     else
23:       unless user_can_vote_poll?(@poll, current_user)
24:         render :action => 'results' and return
25:       end
26:     end
27:   end

REPLACE show Nueva página de una encuesta

[Source]

    # File app/controllers/polls_controller.rb, line 31
31:   def show2    
32:     @title.downcase!
33:     @parent = @poll
34:     @comments = @poll.comments.approved.paginate(:page => params[:page] || 1, :per_page => 50)
35:     if can_edit?("polls") && !user_has_voted_poll?(@poll, current_user)
36:       if @poll.outdated?
37:         render :action => 'results2' and return
38:       end
39:     else
40:       unless user_can_vote_poll?(@poll, current_user)
41:         render :action => 'results2' and return
42:       end
43:     end
44:   end

Guardar el voto y mostrar las respuestas.

[Source]

    # File app/controllers/polls_controller.rb, line 47
47:   def vote
48:     unless  @poll.published?
49:       flash[:notice] = t('admin.polls.Esta_pregunta_no_esta_publicada_y_no_se_puede_votar')
50:       redirect_to poll_path(@poll) and return
51:     end
52:     answer_ids = params[:poll_answer]
53:     #change to today.now - question.start_day
54:     expire_time = 1.year.from_now 
55:     
56:     #find user model from session
57:     user = current_user
58:     view_dir = polls_view_dir
59:     
60:     if answer_ids.blank?
61:       flash[:error] = t('polls.No_ha_elegido_respuesta')
62:       redirect_to poll_url(@poll)
63:     elsif maximum_votes_exceeded(@poll, answer_ids)
64:       flash[:error] = t('polls.Ha_elegido_mas_de_num_respuestas', :num => @poll.max_multiple)
65:       redirect_to poll_url(@poll)
66:     elsif user_has_voted_poll?(@poll, user)
67:       render :file => "#{view_dir}/already_voted.html.erb"
68:     else
69:       # register the vote
70:       PollOption.transaction do
71:         options = PollOption.find(answer_ids)
72:         options.each do |option|
73:           answer = option.answers.build(:ip => request.remote_ip)
74:           answer.user = current_user if logged_in?
75:           answer.save!
76:         end
77:       end
78: 
79:       # save cookie after vote is effective
80:       vote_key = @poll.cookie_name
81:       cookies[vote_key] = {:value => answer_ids.join(", "), :expires => 1.years.from_now }
82:       
83:       # remember this poll as the latest poll voted at to help show results
84:       session[:last_poll] = @poll.id
85:       flash[:notice] = t('polls.Gracias_por_tu_voto')
86:       redirect_to results_poll_url(@poll)
87:     end
88:     
89:   end

[Validate]