简体   繁体   中英

Inserting Controller Actions between Actions in Rails - Best Practices

What are best-practices (or usual-practices) when it comes to adding more steps in a process in Rails?

For example, I am working with the Spree e-commerce Rails platform and I would like to add a multi-step form people should fill out when trying to "Add to Cart" a Product.

The current spree implementation of adding a product to the cart is basically:

ProductsController#show -> OrdersController#edit

That adds the product to the cart, and leaves you at the cart.

So I'm wondering, in order to most-minimally change the core code in spree, how do I make it so the process is more like this:

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

What I'm thinking to do is:

  • modify the "products/show.html.erb" so it goes to surveys_controller.rb. Also modify products_controller.rb to put session[:redirect_to] = order_checkout_path , which I can handle in the SurveysController.
  • or just make those extra things popups, and when I get to the last one, have it call the original method.

What's wrong with that? What is a better approach? This is a question about, more generally, how people go about architecting multistep processes without modifying core code. Not a wizard , just adding extra things in the middle of other things.

Thanks for your help, Lance

You might check out restful_workflow :

This plugin provides interview-style workflow in a controller, similar to TurboTax, but without the need to save models to the database immediately. All data is stored in the session unless overridden.

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.

Have you tried an 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

I'm not 100% sure of the 'add_to_cart' process, but that could be easily deduced by digging through the whole thing in a log. It's a little weird putting your own flow into Spree, but it definitely can be done.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM