2

在过去的 8 年中,我曾尝试使用 Wicked gem 3 次。每一次,我都因为同样的原因放弃了。我再次尝试,因为如果我理解它,我认为它将非常适合我的用例。

我的主要问题是我不明白如何实际启动向导。对于 gem 中使用的示例,它是一个after_registration已经具有关联user对象的事件。这没有帮助,我认为该示例对大多数用例也没有帮助。

还有另一个关于Product在多个步骤中构建 a 的示例。但是,作者未能充分解释路由。来自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.

好的,我不知道句子的第二部分是什么意思,placeholder product_id以及/products/building/build. 我花了 2 个小时尝试,然后转到一个空白的创建表单。

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

这就是我在成功保存 @product 对象后尝试做的事情。

redirect_to product_by_interchange_path(@product, :step1)

那是行不通的。raise InvalidStepError if the_step.nil?说我的步数为零。它不是。

redirect_to product_by_interchange_path(@product, step: :step1)

一样。

redirect_to product_by_interchange_path(:step1)

这是 8 年历史的示例应用程序的精确镜像。但当然@product 不在会话变量current_user中,所以在这种情况下,错误是没有 ID 为 :step1 的产品。

请帮忙!我在这里遗漏了一些非常非常基本的东西,但我非常需要坚持。

4

1 回答 1

1

好的,我终于想通了。这是我所做的:

  1. 首先,我将控制器改回普通的旧控制器ApplicationController并使用了 include include Wicked::Wizard。我不知道这是否有任何作用,但较新的示例与旧示例一样。
  2. 我真的被搞砸了:id。我在想:id通常是我的对象ID。我的控制器中有一个 set_product 私有方法,但它失败了。当我最终发现这:id是实际步骤本身时,这导致我改变了重定向中的路径。
  3. 我将重定向从更改product_by_interchange_path(@product, :select_vehicle)product_by_interchange_path(:select_vehicle, product_id: @product.id)
  4. 我摆脱了我的set_product. 就在我试图消除混乱的时候。
  5. 我将向导中的 finder 调用更改为使用:product_id而不是:id.

现在可以了。我仍然不明白我怎么能用占位符 product_id 删除一条路线,这仍然是个谜。但这很好,而且有效。

于 2020-05-10T22:59:45.163 回答