Created
September 18, 2009 06:02
-
-
Save adzap/188907 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Signup < ActiveRecord::Base | |
validates_presence_of :name, :email_address, :mobile_phone, :username, :password | |
# knocked off variation of something toolmantim posted years ago | |
def valid_for_attributes?(*attrs) | |
return true if valid? | |
attrs.collect! {|attr| attr.to_s } | |
!errors.any? {|attr, error| attrs.include?(attr) } | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Multi step form which validates only submitted attributes per step | |
# but catches all on the final step to make sure model is valid. | |
# Doesn't prevent attribute hacking for each step but will ensure | |
# final record is valid. | |
class Signups < ApplicationController | |
FINAL_STEP = 2 | |
def create | |
@signup = Signup.new(params[:signup] | |
if steps_complete? | |
return redirect_to(elsewhere_path) if @signup.save | |
elsif step_valid? | |
@step += 1 | |
else | |
# not complete or valid renders same step again with errors | |
end | |
# render partial for step in template | |
render :action => 'new' | |
end | |
private | |
def current_step | |
# also upper limit check for homework | |
@step ||= (params[:step] || 1).to_i | |
end | |
def steps_complete? | |
current_step == FINAL_STEP | |
end | |
def step_valid? | |
@signup.valid_for_attributes(params[:signup].keys) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment