[rspec-users] Comments wanted about spec'ing out a couple controller methods

Joe Van Dyk joevandyk at gmail.com
Tue May 22 03:38:05 EDT 2007


(oops, accidentally sent this to the rspec-devel list, sorry about that)

Could someone help me spec out these Rails controller methods?  I
don't know where to get started, especially with the transaction
stuff.  Or, if anyone else could suggest ways I could improve the
code, that would be great as well.

Puzzle has_one Media.  Both are AR model objects.

class PuzzlesController < ApplicationController

 # a before_filter fills in @user

 def create
   @puzzle = Puzzle.new(params[:puzzle])
   @puzzle.user = @user

   @media = Media.new(:uploaded_data => params[:media])

   Puzzle.transaction do
     @puzzle.media = @media
     @puzzle.save! &&  @media.save!
     flash[:notice] = "Success!"
     redirect_to puzzle_path(@puzzle)
   end
 rescue ActiveRecord::RecordInvalid => e
   @media.valid? # force checking of errors even if @puzzle.save!
failed
   flash[:error] = "Problem submitting the One Word Wonder..."
   render :partial => 'new', :layout => true
 end

 def update
   @puzzle = Puzzle.find params[:id]
   Puzzle.transaction do
     @puzzle.attributes = params[:puzzle]
     if params[:media] and params[:media].size > 0
       @puzzle.media = Media.new :uploaded_data => params[:media]
     end
     @puzzle.save!  &&   @puzzle.media.save!
   end
 rescue ActiveRecord::RecordInvalid => e
   @media.valid?
   flash[:error] = "Problem updating the One Word Wonder..."
   render :action => 'edit'
 end

Thanks,
Joe Van Dyk


More information about the rspec-users mailing list