3

在 Rails 的流程中添加更多步骤时,最佳实践(或常规实践)是什么?

例如,我正在使用Spree 电子商务 Rails 平台,我想添加一个人们在尝试“添加到购物车”产品时应该填写的多步骤表单。

当前将产品添加到购物车的大礼包实现基本上是:

ProductsController#show -> OrdersController#edit

这会将产品添加到购物车中,然后将您留在购物车中。

所以我想知道,为了尽可能少地更改 spree 中的核心代码,我该如何做到这一点,所以过程更像这样:

ProductsController#show -> SurveysController#show -> (survey stuff...) -> OrdersController#edit

我想做的是:

  • 修改“products/show.html.erb”,使其进入surveys_controller.rb。还将 products_controller.rb修改为 put session[:redirect_to] = order_checkout_path,我可以在 SurveysController 中处理它。
  • 或者只是弹出那些额外的东西,当我到达最后一个时,让它调用原始方法。

那有什么问题?什么是更好的方法?这是一个关于人们如何在不修改核心代码的情况下构建多步骤流程的问题。不是巫师,只是在其他事情中间添加额外的东西。

谢谢你的帮助,兰斯

4

3 回答 3

1

您可以查看restful_workflow

这个插件在控制器中提供了面试风格的工作流程,类似于 TurboTax,但不需要立即将模型保存到数据库中。除非被覆盖,否则所有数据都存储在会话中。

于 2010-02-14T22:51:57.023 回答
0

It's not a very standard flow in a RESTful web app, so whatever you do will have to be someone hackish, since Rails provides no easy way.

The session variable seems fine to me. Short and simple. You may want to make your key something more descriptive, though, so that you don't see future conflicts.

于 2010-02-08T23:14:16.240 回答
0

你试过orders_controller_decorator.rb吗?

OrdersController.class_eval do
  before_filter :show_survey, :only => :populate
  def new_survey
    #regular stuff

    respond_to do |format|
      format.html do
        #if you've already been through this process, skip the before_filter
        redirect_to(new_order_line_item_url(session[:order_line_item])) and return if session[:order_line_item]
        #otherwise point to the form and store the order data in session 
        session[yeahyougetthepoint]
        render 'new_survey' 
      end
    end
  end
  def create_survey
    #regular stuff
    respond_to do |format|
      format.html {redirect_to new_order_line_item(session[:order_line_item]}
  end
end

我不是 100% 确定“add_to_cart”过程,但可以通过在日志中挖掘整个事情来轻松推断出这一点。将自己的流程放入 Spree 有点奇怪,但绝对可以做到。

于 2011-06-21T12:03:29.697 回答