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[:r开发者_JS百科edirect_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.
精彩评论