2

I have tried using the Wicked gem 3 different times over the past 8 years. Each time, I have given up for the same reason. I'm trying again, because if I understand it, I think it will be perfect for my use case.

My main problem is that I don't understand how to actually begin the wizard. With the example used in the gem, it is an after_registration event that already has an associated user object. That is not helpful, nor do I think that example would be helpful in the majority of use cases.

There is another example about building a Product in multiple steps. However, the author fails to adequately explain the routing. From https://github.com/zombocom/wicked/wiki/Building-Partial-Objects-Step-by-Step:

Since Wicked uses our :id parameter we will need to have a route that also includes :product_id for instance /products/:product_id/build/:id. This is one way to generate that route:

resources :products do
  resources :build, controller: 'products/build'
end

This also means to get to the create action we don't have a product_id yet so we can either create this object in another controller and redirect to the wizard, or we can use a route with a placeholder product_id such as [POST] /products/building/build in order to hit this create action.

OK, I have no idea what the second part of the sentence means as far as placeholder product_id and that route name of /products/building/build. I spent 2 hours trying that and just moved on to a blank create form.

...we can either create this object in another controller and redirect to the wizard

That's what I'm trying to do upon successful save of the @product object.

redirect_to product_by_interchange_path(@product, :step1)

That doesn't work. raise InvalidStepError if the_step.nil? Says my step is nil. It's not.

redirect_to product_by_interchange_path(@product, step: :step1)

Same thing.

redirect_to product_by_interchange_path(:step1)

That's an exact mirror of the 8 year old example app. But of course @product isn't in a session variable like current_user is, so in this case the error is that there's no Product with an id of :step1.

Please help! I am missing something very, very basic here but I very much need to persist.

RubyRedGrapefruit
  • 12,066
  • 16
  • 92
  • 193
  • IT would be very helpful to see your controller actions and routes in a coherent form. otherwise it is very difficult to debug your issue. wicked is fairly straightforward - this problem should be easily solvable – BenKoshy May 10 '20 at 22:52

1 Answers1

1

OK I have finally figured this out. Here's what I did:

  1. First of all, I changed my controller back to a plain old ApplicationController and used the include include Wicked::Wizard. I don't know if that did anything, but the newer example was laid out like the old.
  2. I was really screwed up by :id. I'm thinking :id is generally my object ID. I had a set_product private method in my controller, and it was failing. When I finally figured out that :id was the actual step itself, that led me to change my path in the redirect.
  3. I changed the redirect from product_by_interchange_path(@product, :select_vehicle) to product_by_interchange_path(:select_vehicle, product_id: @product.id)
  4. I got rid of my set_product. Just while I was trying to eliminate confusion.
  5. I changed my finder calls in the wizard to use :product_id instead of :id.

It works now. I still don't understand how I could have stubbed out a route with a placeholder product_id, that's still a mystery. But this is fine and it works.

RubyRedGrapefruit
  • 12,066
  • 16
  • 92
  • 193