4

I have a multi-step form where the user fills out info on several different pages. In conventional rails, you keep each resource separate in its own controller and you use the REST actions to manipulate the data.

In the conventional system I would have 3-5 different controllers (some steps are optional) for a single multi-step form. There's no real sense of "order" in the controllers if I do it the conventional way. A new developer coming on to the project has to learn what steps map to what steps and so forth.

On the other hand, I have thought about breaking convention and having a single controller that organizes the entire multi-step form. This controller would be full of methods like:

def personal_info
  # code...
end

def person_info_update
  # code...
end

def residence_info
  # code...
end

def residence_info_update
  # code...
end

# many more coupled methods like the above...

This single controller will get fairly long, but it's essentially a bunch of coupled methods: one for showing the step (form) and the other for updating and redirecting to the next step.

This would be breaking rails convention and I would have to setup my own routing.

But I'm curious how others have solved this problem? I know both CAN work, but I would like to know which is easier to maintain and code with in the long run.

Dan L
  • 4,319
  • 5
  • 41
  • 74

2 Answers2

0

A resource does not equal a page. I suspect that both ways would break a constraint on REST.

All of your interests have been with the View domain, which resides in your browser. If you want to display a single form in multiple parts you should do so using HTML, CSS etc.

Otherwise your just creating temporary storage on your servers for the forms progress.

Michael Brown
  • 498
  • 4
  • 13
0

I did something like this with https://github.com/pluginaweek/state_machine

The idea was to have one state per step of the form and simply render a different form partial depending on which state the actual resource has. The above gem let's you specify validations and callbacks for each states.

Like this, you can use the standard REST controller actions.

moritz
  • 25,477
  • 3
  • 41
  • 36