Skip to content

Instantly share code, notes, and snippets.

@softwaregravy
Created August 7, 2011 03:26
Show Gist options
  • Save softwaregravy/1130026 to your computer and use it in GitHub Desktop.
Save softwaregravy/1130026 to your computer and use it in GitHub Desktop.
Using a state machine will give you a messy update
def update
@registration = Registration.find(params[:id])
redirect_to users_path(@registration.user_id) and return if @registration.state == 'complete'
if @registration.update_attributes(params[:registration])
if @registration.next!
if @registration.state == 'complete'
#can only reach this block on first completion -- or next will have failed
sign_in(:user, @registration.user)
redirect_to user_show_path and return
else
redirect_to registration_state_path(@registration, @registration.state) and return
end
else
flash[:alert] = @registration.errors
render :template => get_template_for_state(@registration, @registration.state) and return
end
else
flash[:alert] = @registration.errors
respond_with(@registration, :location => registration_state_path(@registration, @registration.state))
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment