Skip to content

Instantly share code, notes, and snippets.

@danhere
Created April 24, 2011 15:53
Show Gist options
  • Save danhere/939643 to your computer and use it in GitHub Desktop.
Save danhere/939643 to your computer and use it in GitHub Desktop.
Results controller
class ResultsController < ApplicationController
before_filter :authenticate_admin!, :except => [:index, :show]
caches_page :show
respond_to :html, :xml
def index
@results = Result.find(:all, :order => 'created_at DESC').paginate(:page => params[:page])
end
def show
@result = Result.find_by_short_url(params[:id]) || Result.find_by_id(params[:id])
respond_with(@result, :location => @result)
end
def new
@result = Result.new
end
def create
@result = Result.create(params[:result])
if @result.save
flash[:success] = 'Result uploaded successfully!'
respond_with(@result, :location => @result)
else
flash[:error] = 'Upload failed.'
respond_with(@result, :location => new_result_path)
end
end
def edit
@result = Result.find_by_short_url(params[:id]) || Result.find_by_id(params[:id])
end
def update
@result = Result.find_by_short_url(params[:id]) || Result.find_by_id(params[:id])
if @result.update_attributes(params[:result])
flash[:success] = 'Result was successfully updated.'
respond_with(@result, :location => results_path)
expire_page :action => :show
else
flash[:error] = 'Oh snap. The hampsters stopped running. Try again.'
respond_with(@result, :location => edit_result_path)
end
end
def destroy
@result = Result.find_by_short_url(params[:id]) || Result.find_by_id(params[:id])
@result.destroy
flash[:success] = "The result was terminated. Pew pew pew."
respond_with(nil, :location => results_path)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment