DEV Community

Raghu Betina
Raghu Betina

Posted on

Building a multi-step form in Rails with Wicked

I wanted to make a wizard to help students visualize the relationships between their database tables, so I had a chance to play with the Wicked gem. Here is the work-in-progress:

https://association-accessors.firstdraft.com/

The Wicked gem has a good README but the example within centers around a signup form. Since my wizard is not for a user model (and thus doesn’t have current_user), and since I have validations that I wanted to enforce at some steps but not others, I found this blog post very helpful:

https://www.joshmcarthur.com/2014/12/23/rails-multistep-forms.html

That post got me 99% of the way, but a few additional notes:

  • My wizard has conditional logic; it's not a linear path. So my update action has some jump_tos:

    if step == 'nature' && @association.indirect?
      jump_to(:join_table)
      render_wizard
    elsif step == 'foreign_key' && @association.valid?
      jump_to(:name)
      render_wizard
    else
      render_wizard @association
    end
    

    It will be interesting to think about how to generalize this for more complicated wizards. Do we capture the logic in a full-on state machine, and use that to decide where to jump_to?

  • I re-rendered previous form fields and disabled them to remind the user what they'd already entered, so breaking each form-group into its own partial was very helpful.

Top comments (1)

Collapse
 
clint326 profile image
Clint Laskowski

Hi, @raghubetina! Nice to see you here on Dev.to. I'm exploring the Wicked Wizard Gem for a new project. Thanks for this post. I'll update you on my efforts (possibly through other channels).