Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7091221
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T08:09:27+00:00 2026-05-28T08:09:27+00:00

I am writing a wizard form in rails; e.g. multiple input pages for one

  • 0

I am writing a wizard form in rails; e.g. multiple input pages for one model object.

The basics of my approach are those described in Ryan Bates’ Multistep form railscast : http://railscasts.com/episodes/217-multistep-forms (in case anyone wants to know the WHY behind some of the code below).

The objects under scrutiny here are “Participant”, which has one “Address”

My problem is that I only want to validate the nested object (Address) when the user is trying to progress past the address input screen. This is currently tracked via an attribute on the Participant model called “current_step”

So I have a Participant:

class Participant < ActiveRecord::Base
  has_one :address
  accepts_nested_attributes_for :address
  validates_presence_of :first_name, :last_name, :if => self.current_step == "name"
  ...
  def steps = %w[ name address confirm ] # the steps the wizard will follow
end

And an Address:

class Address < ActiveRecord::Base
  belongs_to :participant
  validates_presence_of :address1, :state, :suburb, :postcode #, :if => participant.current_step == "address"
end

The principle of this approach is the “create” action is called on the controller (not shown) for each step of the wizard, and it only validates a subset of the model when each step is processed.

Currently, when I complete the first screen (“name”) and try and go onto the address step, the address validation is getting triggered, and I get sent back to the “name” screen with validation errors for blank address details.

So I have tried a number of approaches here, the final part of which was the commented out condition on Address validation shown above – this I’ve found doesn’t work as I’m only building Participant->Address objects, but not saving them. Therefore @participant.address gets me the address object, but @participant.address.participant is null as the Address does not yet have a participant_id foreign key to lookup it’s parent.

The reason for my struggles appears to be the inclusion of the super-handy accepts_nested_attributes_for method. I was expecting to use a validates_associated to be doing the validation, but I see that the accepts_nested_attributes_for tag both propagates form parameters nicely to create nested model objects, but also ensures the participant#valid? method calls down to the address validation in ALL situations.

So my dilemma is how to best use the participant#valid? method to valid the partially complete model, based off the current_step parameter in the participant ?

EDIT – updated to remove extra info, and distill down to the core problem

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T08:09:28+00:00Added an answer on May 28, 2026 at 8:09 am

    Add a virtual attribute on your Address model:

    class Address < ActiveRecord::Base
      belongs_to :participant
      attr_accessor :skip_validation
      validates_presence_of :address1, :state, :suburb, :postcode, 
                               :unless => :skip_validation 
    end
    

    Set the virtual attribute on the address object when current_step is set.

    class Participant < ActiveRecord::Base
      has_one :address
      accepts_nested_attributes_for :address
      attr_accessor :current_step
      validates_presence_of :first_name, :last_name, 
                             :if => lambda {|r| r.current_step == "name"}
    
    
      def current_step=(value)
        unless (value == "address")
          address.skip_validation = true
        end    
        @current_step = value 
      end    
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm writing a wizard UI based on the QWizard Qt object. There's one particular
I am writing a form wizard using JQuery's accordion module . The problem is
I'm writing an app that has a wizard type input section. Think MS Windows
I'm writing a wizard for an Eclipse RCP application. After doing some processing on
I am currently writing a wizard-style application using Qt4. I am not using the
I am writing a wizard-style application in Qt that uses a QStackedWidget to organize
I have an app that I'm writing a little wizard for. It automated a
I've got a guy who is a wizard at writing stored procs and he
I am writing a basic wizard for my web site. It will have 4
Say I have this entity with a lot of attributes. In the input form

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.