Tim Brunsmo
Tim Brunsmo

Reputation: 601

Constructing a wizard in Ruby on Rails

So I am trying to build a wizard for my users on sign up. I cannot use javascript (What I found is the general solution for this kind of forms.) The reason is that every page in my wizard is heavy Javascript dependent, which means that loading all at once would take to long time. And oh, the wizard deals with 3 different resources.

I've tried one approach that I'm currently stuck on, and frankly doesn't feel right.

(SIDENOTE: user is exposed with "decent-exposure" gem)

This is my wizard-controller:

  def index
    case current_step
      when 'description'
        render 'users/edit'
      when 'contact'
        render 'contact_informations/edit'
      when 'location'
        render 'location/edit'
    end
  end

  def current_step
    @current_step || STEPS.first
  end

  def next_step
    @current_step = STEPS[STEPS.index(current_step)+1]
    index
  end

  def previous_step
    @current_step = STEPS[STEPS.index(current_step)-1]
    index
  end

  def final_step
    @current_step == STEPS.last
  end

Then I have one update action for each of the resources(this is the same wizard-controller)

 def update_description

    if user.update_attributes(params[:user])
      next_step
    else
      redirect_to user_wizard_path(user)
    end
  end

  def update_contact_information
    # Do stuff here
  end

  def update_location
    # Do stuff here
  end

It seems like there is a problem with calling actions from within the same controller. And based on that I've never seen this be done before, it feels wrong.

What I am trying to accomplish in simple: I do not want to clutter up the original REST-controllers of resources. I want every step to be validated If update_attributes fails, re-render form with errors. If update_attributes succeed render next step.

I've come to the conclusion that my shot at this will not work, and does not feel like the "rails-way" of doing things. I've watched Ryan Bates railscast on "multiple-step-forms" but even after that, I can't seem to figure this out.

Upvotes: 2

Views: 1336

Answers (1)

jalagrange
jalagrange

Reputation: 2381

check out the wicked gem, you might find it helpful

https://github.com/schneems/wicked

Upvotes: 3

Related Questions