Create a wizard that works with multiple resources
I am writing an application that has a "master" type input section. Think about MS Windows installers.
I'm having a hard time figuring out what is the most efficient way to do this with rails. I can do this to no avail (already for version 1 of the app), but this time I'm trying to be a little more idiomatic.
Here's the situation. I have a 5-step wizard that needs to gather information. Steps 1, 2 and 4 refer to resource A. Step 3 refers to multiples of resource B and must associate them with resource A. Step 5 is just confirmation.
So I have my resourceA_controller and my resourceB_controller ... but they only save / update one resource. I am guessing that I have to set up a wizard_controller for each of the steps in the process, but I'm not sure how the routing is supposed to work.
For example...
WizardController < ApplicationController
def stepOne
@resourceA = ResourceA.new
end
def stepTwo
@resourceA = params[:id]
end
...
def stepFive
end
end
And then I would look at the StepOne view:
<form action='/resourceA/new'/>
and StepTwo actions
<form action='/resourceA/12345/edit'/>
and etc.
But then my resourceA and resourceB controllers would have to know how to redirect to the appropriate step in wizard_controller. A tangled mess!
Am I anywhere near the correct track? Or is there a rails built-in mechanism or plugin that does things like this.
source to share
It's important to understand the difference between what REST provides and what REST doesn't care about.
-
A RESTful service provides a minimal set of actions that a client can use (as long as it knows the correct data format) to manage the resource class
- For example, you must be able to POST to
/resourceA/12345
edit this existing resource
- For example, you must be able to POST to
-
The RESTful service makes no guarantees as to what other URLs will return meaningful responses.
-
One notable example is that REST does not specify that it
/resourceA/12345/edit
will return an HTML form intended to edit that resource. This feature of the HTML application simply provides the way to do the POST described above. -
Expanding on this theory, it is perfectly acceptable to have multiple edit forms that all POST match a specific RESTful URL. Since for mass control the
edit
rule isedit
usually usededit
to mass assign what it passed, you can look at the passed attributes as well as information about which HTML button was used to submit the form to decide which page the user should see next. -
It might be scary to rely on a single controller method to deal with multiple page views, but with smart validation and selective rewriting of accessories, you can maintain a lot of control over how users access your application.
/ li> -
You should also be able to decide that this method does not restrict users to only submitting attributes in the order shown on the form. Another RESTful client could, in theory, update the POST for all the attributes associated with a resource, not just those represented by one page of your wizard. Assuming your model methods are robust enough, anything can be done for Just Work.
-
source to share
You can try this JQuery Wizard plugin :
http://worcesterwideweb.com/2007/06/04/jquery-wizard-plugin/
There is a demo here:
source to share