Topic: View/Controller that needs to handle multiple "steps"

Time to up the difficulty of the applications I am building. Here are the details:

I want to build a new "user". This user has regular attributes (name, login, password etc.). This user also has "permissions" to access certain items. When creating this user I also want to be able to set "feature_permissions" to allow this user to have access to certain features.

So, there are three different models that this user creation process needs to touch. I want to be able to create this user in a three step process:

step 1: Set the default user values
step 2: Set the permissions for the items for this user
step 3: Set the feature permissions for this user

I also don't want to have partially created users in the database, so I don't want to write this information to the database until step 3 is completed.

Now, I don't want to create messy code by having a single "user" controller that does CRUD functions on three (seemingly) different things. The obvious way to attack this is to have a "user" controller, a "item_permissions" controller, and a "feature_permissions" controller, but I am not totally sure how to link them together so that the information persists between the three controllers and then at the end of step three, be able to write the information to the proper tables in the database.

Advice would be appreciated.

Re: View/Controller that needs to handle multiple "steps"

Seems simple enough to have a single view onto the three related tables (associated models).

Re: View/Controller that needs to handle multiple "steps"

I guess the question is "how"? Getting the data together is stumping me. I am not sure what the "Rails way" of doing something like this is...

Re: View/Controller that needs to handle multiple "steps"

I had a similar problem, but I solved it by creating a series of "steps" (read pages) that acted/looked like they were part of a wizard, e.g. "step 1 of 4" this gave the impression to the user that each step has to be completed before anything was committed.

I chose this visual approach becuase as "users" ourselves we have been conditioned to complete all steps of a wizard. The classic example is an installation wizard.

Hope this approach helps,

Steve.

Re: View/Controller that needs to handle multiple "steps"

So how do you deal with users abandoning the process half way through. Won't you have invalid data in your models/tables?

Re: View/Controller that needs to handle multiple "steps"

PhilThompson wrote:

So how do you deal with users abandoning the process half way through. Won't you have invalid data in your models/tables?

My solution was:

From the users point -

I display the "next activity" that needs completing for each item the user was "in the process of completing - but didn't for some reason" on their home page. This acts as a visual reminder to the user that it needs completing.

I could quite easily add some code that "stops" the user from "adding" more items/entries until either a) they complete the current process or b) they delete the item/entry.

From the db/application point -

On initial creation of the item, I generate an ID within the D/B plus some non-critical info, e.g. notes, date/time stamp, etc.

Then each "step" of my wizard adds a little more flesh to the item, until the item is fully fleshed and available for use. Of course all data is validated before any d/b updates.

I agree, with my current implementation, there is nothing stopping a user from creating multiple "orphan" entries in the d/b - but these would be quite easy to clean up, for example I could create a sweeper to scan the d/b and remove the relevant entries and post a message to the user informing them of said operation.

Hope this answers your query,

Steve.

Re: View/Controller that needs to handle multiple "steps"

Orphan sessions and such is the reason why I liked the idea of putting all of the information from each steps into a session, and then at the last step writing the information to the db. Just make sure to reset the session information at "step 1" every time, and make sure to not allow them to go to "step 2" without going to "step 1" first.

Re: View/Controller that needs to handle multiple "steps"

Both are valid implementations. I like the idea of beings able to come back later and finish a wizard for example, devonps. When this is not viable then using sessions or hidden fields are valid alternatives.

I found another post on this forum that indicates how you'd do validation by putting a condition on the model validations so it will only validate that field if on a certain page. I like this approach.

Re: View/Controller that needs to handle multiple "steps"

True, they both have their uses. Can you link me up to that post that you found? It would be useful. Thanks.

Re: View/Controller that needs to handle multiple "steps"

Here you go,

plugin: acts as wizard

http://blog.seesaw.it/articles/2006/05/ … _as_wizard

http://danielfischer.com - Personal Web-Technology-Blog, Los Angeles.

Re: View/Controller that needs to handle multiple "steps"

I'm not usually one for swearing, but acts_as_wizard is f-ing brilliant. Thanks!

Re: View/Controller that needs to handle multiple "steps"

Found it, here you go : http://railsforum.com/viewtopic.php?id=721

Re: View/Controller that needs to handle multiple "steps"

I had a quick look at that but I couldn't tell whether it just adds some kind of navigation/breadcrumbs thing or if it really handles issues like multi-page validation and persistence.

Re: View/Controller that needs to handle multiple "steps"

I think from a usability standpoint it would be much easier for the person using your application to do all three steps on one page all at once.  You don't have to bombard them with choices but just show them the essential information that much be entered for the user to be successfully created.  If need be they can go back and update the users permissions at a later time.

Re: View/Controller that needs to handle multiple "steps"

Sometimes you have to do things in two steps with Rails though. Usually it is a case of something relying on the value of another to be generated. Well, that is why I am doing it in any case...

Re: View/Controller that needs to handle multiple "steps"

Ceres wrote:

Sometimes you have to do things in two steps with Rails though. Usually it is a case of something relying on the value of another to be generated. Well, that is why I am doing it in any case...

This might be a perfect place to use AJAX.