Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
# This is my home-grown solution for implementing the model end of
# a multi-step form in Rails.
#
# There's a step attribute on the model, and the after_step method
# lets me limit validations to only occur after a particular step.
#
# I have to be careful in the controller to make sure the right
# attributes are set at each step, and that the step attribute can't
# be hacked.
class Signup < ActiveRecord::Base
def self.after_step(n, &block)
with_options(:if => lambda {|signup| signup.step >= n }) do |signup|
signup.instance_eval(&block)
end
end
after_step(1) do
validates_presence_of :name, :email_address, :mobile_phone
end
after_step(2) do
validates_presence_of :username, :password
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment