Added the ability to do ajax adding of candidates so there is no longer
[selectricity] / app / controllers / quickvote_controller.rb
index c16755a0a8636da9d8598f2bc65edc9afa004a6e..97f4ab05fb726bd0a03cdce10ee88c025cbbfd98 100644 (file)
@@ -6,23 +6,51 @@ class QuickvoteController < ApplicationController
 
   def index
     @election = QuickVote.find_all(["name = ?", params[:votename]])[0]
-    
-    @voter = QuickVoter.find_all(["session_id = ? and election_id = ?",
+
+    if @election
+      @voter = QuickVoter.find_all(["session_id = ? and election_id = ?",
                                   session.session_id, @election.id])[0]
-    unless @voter 
-      @voter = QuickVoter.new
-      @voter.election = QuickVote.find_all( [ "name = ?", params[:votename] ] )[0]
+      unless @voter 
+        @voter = QuickVoter.new
+        @voter.election = QuickVote.find_all( [ "name = ?", params[:votename] ] )[0]
+      end
+    else
+      redirect_to :controller => 'site'
     end
   end
 
   def create
     if params[:quickvote] 
       @quickvote = QuickVote.new(params[:quickvote])
-      if @quickvote.reviewed? and @quickvote.save
-          @quickvote = @quickvote.reload
-         render :action => 'success'
+
+      # store the candidate grabbed through ajax and stored in flash
+      @quickvote.candidatelist = flash[:candlist]
+
+      # try to save, if it fails, show the page again (the flash should
+      # still be intact
+      if @quickvote.save
+        @quickvote = @quickvote.reload
+        render :action => 'success'
+      else
+        flash.keep(:candlist)
       end 
+    else
+      # if we don't have a quickvote param, it means that the person
+      # here has not been hitting this page and we can clear any
+      # candlist in the flash
+      flash.delete(:candlist) if flash.has_key?(:candlist)
+    end
+  end
+
+  def add_candidate
+    candidate_name = params[:ajax][:newcandidate]
+    if flash.has_key?(:candlist) and flash[:candlist].instance_of?(Array) 
+      flash[:candlist] << candidate_name
+    else
+      flash[:candlist] = [ candidate_name ]
     end
+    flash.keep(:candlist)
+    render_partial 'candidate_list'
   end
 
   def change

Benjamin Mako Hill || Want to submit a patch?