box-o-sand/rails/map-mash/app/controllers/mashes_controller.rb

52 lines
1.2 KiB
Ruby
Raw Normal View History

2012-03-04 16:51:35 +00:00
class MashesController < ApplicationController
def redirect_to_new
flash[:notice] = 'Sneaky pete.'
redirect_to(:action => 'new')
2012-03-04 16:51:35 +00:00
end
alias_method :index, :redirect_to_new
alias_method :show, :redirect_to_new
2012-03-04 16:51:35 +00:00
def new
@map_a, @map_b = Map.pair
@mash = Mash.new(
:map_a => @map_a,
:map_b => @map_b,
:winner => @map_a
)
2012-03-04 16:51:35 +00:00
respond_to do |format|
format.html
2012-03-04 16:51:35 +00:00
end
end
def create
mash_params = params[:mash].clone
logger.info("Got params: #{params.inspect}")
requester = Requester.find_or_initialize_by_ip(request.remote_ip)
requester.save!
requester.reload
logger.info("Setting mash.requester_id from #{requester.inspect}")
logger.info("Creating mash with: #{mash_params.inspect}")
@mash = Mash.new(mash_params)
@mash.requester_id = requester.id
@winner = Map.find(@mash.winner_id.to_i)
@winner.points += 1
2012-03-04 16:51:35 +00:00
logger.info("About to save #{@mash.inspect}")
2012-03-04 16:51:35 +00:00
respond_to do |format|
if @winner.save && @mash.save
2012-03-04 16:51:35 +00:00
flash[:notice] = 'Mash was successfully created.'
format.html { redirect_to(:action => 'new') }
2012-03-04 16:51:35 +00:00
else
format.html { render :action => 'new' }
2012-03-04 16:51:35 +00:00
end
end
end
end