From 70d0a835ed0eb6824b53693e891db76010960524 Mon Sep 17 00:00:00 2001 From: Dan Buch Date: Wed, 7 Mar 2012 13:46:19 -0500 Subject: [PATCH] Handling mash creation, removing some unnecessary controller methods, redirecting some others to :action => 'new'. --- .../app/controllers/mashes_controller.rb | 60 ++++--------------- rails/map-mash/app/views/mashes/new.html.erb | 28 ++++----- 2 files changed, 25 insertions(+), 63 deletions(-) diff --git a/rails/map-mash/app/controllers/mashes_controller.rb b/rails/map-mash/app/controllers/mashes_controller.rb index 76a9dc6..79fc3b7 100644 --- a/rails/map-mash/app/controllers/mashes_controller.rb +++ b/rails/map-mash/app/controllers/mashes_controller.rb @@ -1,22 +1,11 @@ class MashesController < ApplicationController - - def index - @mashes = Mash.all - - respond_to do |format| - format.html - format.xml { render :xml => @mashes } - end + def redirect_to_new + flash[:notice] = 'Sneaky pete.' + redirect_to(:action => 'new') end - def show - @mash = Mash.find(params[:id]) - - respond_to do |format| - format.html - format.xml { render :xml => @mash } - end - end + alias_method :index, :redirect_to_new + alias_method :show, :redirect_to_new def new @map_a, @map_b = Map.rand(2) @@ -25,7 +14,7 @@ class MashesController < ApplicationController :requester => request.remote_ip, :map_a => @map_a.id, :map_b => @map_b.id, - :winner => 'a' + :winner => @map_a.id ) respond_to do |format| @@ -34,47 +23,20 @@ class MashesController < ApplicationController end end - def edit - @mash = Mash.find(params[:id]) - end - def create - @mash = Mash.new(params[:mash]) + mash_params = params[:mash] + mash_params[:requester] = request.remote_ip + @mash = Mash.new(mash_params) respond_to do |format| if @mash.save flash[:notice] = 'Mash was successfully created.' - format.html { redirect_to(@mash) } + format.html { redirect_to(:action => 'new') } format.xml { render :xml => @mash, :status => :created, :location => @mash } else - format.html { render :action => "new" } + format.html { render :action => 'new' } format.xml { render :xml => @mash.errors, :status => :unprocessable_entity } end end end - - def update - @mash = Mash.find(params[:id]) - - respond_to do |format| - if @mash.update_attributes(params[:mash]) - flash[:notice] = 'Mash was successfully updated.' - format.html { redirect_to(@mash) } - format.xml { head :ok } - else - format.html { render :action => "edit" } - format.xml { render :xml => @mash.errors, :status => :unprocessable_entity } - end - end - end - - def destroy - @mash = Mash.find(params[:id]) - @mash.destroy - - respond_to do |format| - format.html { redirect_to(mashes_url) } - format.xml { head :ok } - end - end end diff --git a/rails/map-mash/app/views/mashes/new.html.erb b/rails/map-mash/app/views/mashes/new.html.erb index 7dcddce..bcbd85b 100644 --- a/rails/map-mash/app/views/mashes/new.html.erb +++ b/rails/map-mash/app/views/mashes/new.html.erb @@ -1,24 +1,24 @@ -

New mash

+

Mash the Maps!

+

Choose your fave.

<% form_for(@mash) do |f| %> <%= f.error_messages %> +
+ <%= f.label 'A' %>
+ <%= f.hidden_field :map_a, :value => @map_a.id %> + +
+
+ <%= f.label 'B' %>
+ <%= f.hidden_field :map_b, :value => @map_b.id %> + +

-

- <%= f.label :map_a %>
- -
-
- <%= f.label :map_b %>
- -
+ <%= f.select :winner, [['A', @map_a.id], ['B', @map_b.id]] %>

- <%= f.label :winner %>
- <%= f.text_field :winner %> -

-

- <%= f.submit 'Create' %> + <%= f.submit 'Mash' %>

<% end %>