1

Context

I'm new to app development in Rails so please let me know if I am missing something obvious about saving/storing states in Rails. I'm using the Wicked gem in my app to guide users through a medical assessment that has the following steps:

  1. Personal information
  2. General medical questions
  3. Specific disease questions
  4. Consent
  5. Submit

Problem

If users leave at a certain part of the assessment to check another page of the app, or if they accidentally close the browser, they lose all their progress.


What I have tried

  • I added a save button on every step that users can click to save the information they have entered so far, and upon leaving and coming back, they would have the information pre-filled for them. This is not the UX I'm looking for, however, because users will still have to start from the first step and just keep clicking next until they reach the step they were last on or use the navigation to find the step. I want the wizard to automatically open up where they left off.

  • I tried this approach where I add an attribute for the user called :assessment_step and I update it every time the user reaches a new step and go to the step if it exists whenever the user opens the wizard. This approach works but is very hacky and not scalable at all. As I add more steps, and more wizards, this will be a pain to maintain. Here is the code:

    class MedicalAssessmentController < ApplicationController
      include Wicked::Wizard
    
      steps :personal_info, :medical_questions, :disease_questions, :consent, :submit
    
      def show
        @user = current_user # default assessment step is 0
        @steps = [:personal_info, :medical_questions, :disease_questions, :consent, :submit]
        case step
        when :personal_info
          if @user.assessment_step != 0:
            jump_to(@steps[@user.assessment_step])
          @user.update_attribute(:assessment_step, 0) # set the step reached to step 0
        when :medical_questions
          @user.update_attribute(:assessment_step, 1)
        when :disease_questions
          @user.update_attribute(:assessment_step, 2)
        when :consent
          @user.update_attribute(:assessment_step, 3)
        when :submit
          @user.update_attribute(:assessment_step, 4)
        end
        # DO MORE STUFF
        render_wizard
      end
    end
    

Things I read

I couldn't find any similar questions on SO or anywhere online. The Wicked gem docs don't really mention anything similar.

oamandawi
  • 405
  • 5
  • 15
  • 1
    You could look into caching where the user is and if there's a value for the specific key, then show that step or else start over – Int'l Man Of Coding Mystery Apr 12 '21 at 21:26
  • Thank you for this suggestion! Do you mind elaborating further in answer on what that would look like or any resources I can look into? – oamandawi Apr 12 '21 at 22:49
  • 1
    These might good resources. You can set a key with the user id, and the wicked step they're on and update it when they get to the next step. You can set the expire to what ever you want. This way when the user comes back you've saved the step they're on and can direct them accordingly https://www.honeybadger.io/blog/ruby-rails-view-caching/ and https://guides.rubyonrails.org/caching_with_rails.html – Int'l Man Of Coding Mystery Apr 13 '21 at 14:01
  • That's awesome; thank you : ) – oamandawi Apr 13 '21 at 15:50
  • Wondering if you found a solution to this? I am going to embark on using the wicked gem today, and your question is one that I have. Would be great to have an answer posted. Thanks. – 3BB Feb 24 '22 at 20:05
  • Hi @BryanBeshore I don't have a good answer to this besides caching. If you don't want to use caching, I suggest posting an issue on the wicked repo: https://github.com/zombocom/wicked/issues. Hopefully, they can have a better response. I personally abandoned this feature for my project because it became hard to maintain. – oamandawi Feb 25 '22 at 03:08

0 Answers0