Skip to content

Instantly share code, notes, and snippets.

@mikew
Created June 7, 2010 19:14
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mikew/429048 to your computer and use it in GitHub Desktop.
Save mikew/429048 to your computer and use it in GitHub Desktop.
- form_for @user do |f|
= f.hidden_field :signup_step, :value => @user.signup_step
= render :partial => "step_#{@user.signup_step}", :locals => { :f => f }
= f.submit
class User < ActiveRecord::Base
attr_accessor :signup_step
validates_presence_of :first_name, :last_name, :billing_address
end
class RegistrationsController < ApplicationController
STEP_VALIDATIONS = [
%w(first_name last_name),
%w(billing_address)
]
def new
@user = User.new
end
def create
@user = user_from_session
increment_signup_step @user, params[:reg]
respond_to do |format|
if @user.signup_step == STEP_VALIDATIONS.length && @user.save
format.any { render :final, :status => 202 }
else
format.html { render :new }
format.any(:js, :json) { render :json => @user.errors, :status => @user.errors.empty? ? 202 : 400 }
end
end
end
private
def increment_signup_step(model, params)
model.attributes = params
model.signup_step += 1 if valid_for_attributes? model, *STEP_VALIDATIONS[model.signup_step]
end
def valid_for_attributes?(model, *attrs)
return true if model.valid?
errors = model.errors.dup
model.errors.clear
errors.each do |attr, message|
model.errors.add(attr, message) if attrs.include? attr
end
return model.errors.empty?
end
def user_from_session
session[:temp_user] ||= User.new
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment