DEV Community

Raghu Betina
Raghu Betina

Posted on

4 1

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.

Heroku

Build apps, not infrastructure.

Dealing with servers, hardware, and infrastructure can take up your valuable time. Discover the benefits of Heroku, the PaaS of choice for developers since 2007.

Visit Site

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).

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs