Inserting Controller Actions Between Actions in Rails - Best Practices

What are the best practices (or common practices) when it comes to adding extra steps to a process in Rails?

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

Currently, the implementation of adding a product to the basket is mainly:

ProductsController#show -> OrdersController#edit

This adds the product to the basket and leaves you in the basket.

So, Iโ€™m interested in order to change the base code in spree as much as possible, how to do this, so that the process looks more like this:

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

What I'm going to do is:

  • change "products / show.html.erb" to go to survey_controller.rb. Also modify products_controller.rb to supply session[:redirect_to] = order_checkout_pathwhich I can handle in SurveysController.
  • or just create these extra pop-ups, and when I get to the last, ask him to call the original method.

What is wrong with that? Which approach is better? This is a question, in general, about how people go to the architecture of multi-stage processes without changing the kernel code. Not a wizard , just adding extra things in the middle of other things.

Thanks for your help, Spear

+3
source share
3 answers

restful_workflow:

, TurboTax, . .

+1

- RESTful, , , - , Rails .

, . . , , .

0

order_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, .

0

Source: https://habr.com/ru/post/1731940/