jeffp / wizardly

create a functioning wizard for any model in three steps
http://github.com/jeffp/wizardly-examples
MIT License
131 stars 16 forks source link

How can I edit an existing resource on a specific stage? #2

Open rubenfonseca opened 15 years ago

rubenfonseca commented 15 years ago

Hi! Thanks for your plugin.

How can I edit an existing resource on a specific stage? Imagine I have the stages :stage1, :stage2, :stage3. I fill all the stages and the object is created. I then want to re-edit on :stage2. Is it possible?

jeffp commented 15 years ago

Easiest is to create a :stage4, and save the model instance in :stage3:

on_next(:stage3) do
  @model_name.save
end

Otherwise, you'll have to set up some routing. Try the following.

on_finish(:stage3) do
  do_not_complete
  @model_name.finished = true
  @model_name.save  #or save_without_validation
  redirect_to :stage2
end

on_next(:stage2) do
  if @model_name.finished
    complete_wizard(your_redirect_here)
  end
end

In this method, you'll have to add a field to detect that :stage3 has been completed, I called it 'finished' and used a boolean defaulting to false. The 'do_not_complete' method tells the wizard to maintain its knowledge of the current model when the user presses finish, this allows you to re-enter :stage2 and work on the same model instance. We call 'complete_wizard' here if this is the second time through.

Let me know what works.

oliverbarnes commented 14 years ago

Hi Jeff,

Would there be a way to edit an existing resource without depending on an existing session? say a user decides to complete the form from another computer, thus creating a new session, and I give him a login owning the resource - is there a way to pass this resource to the wizardly controller?