Skip to content

Show dynamic progress step counter to user

Benoit Tigeot edited this page Feb 10, 2021 · 6 revisions

I created a helper method to quickly show a step count to the end user. The method feels a bit dirty (I think some Rubyists here might be able to clean this up) but it gets the job done. The objective of this code is to output a dynamic progress counter ("4/13") so it can be used in a context like "Question Set 4/13".

  1. Put this method in your ...StepsHelper.rb file
  def step_progress
    # get current step's relative position
    step_index = wizard_steps.each_with_index.select { |i, idx| i =~ /#{step}/}
    step_index_results = step_index.map! { |i| i[1] }
    # nb// adjust to account for array starting index at 0
    current_step_position = step_index_results.first + 1 

    # get total number of steps
    # nb// adjust to account for array starting index at 0
    total_step_count = wizard_steps.size + 1

    "#{current_step_position}/#{total_step_count}"
  end
  1. In your view call "step_progress"