ambertch
ambertch

Reputation: 7649

What's a good way to interact with Spree's cart/products from Rails app?

I'm bolting Spree onto an existing app. In my app's pages, to display a subset of products I'm currently using the taxon id to access Spree::Product. Same with orders: from the app code I do "Spree::Order.find(session[:order_id])" (so I can display # of items in cart outside of Spree pages)

This seems heavy handed... should I be customizing Spree instead (rewrite some pages), or spinning it up as a separate instance and making API calls, etc.?

Thanks!

Upvotes: 1

Views: 1530

Answers (2)

m_x
m_x

Reputation: 12564

Your question is not very clear (i don't know exactly what you're trying to achieve) and i don't know Spree well, but here are some elements of answer.

  • to override a view (usually works with most namespaced plugins and engines), just drop a file with the same name and path in your main app tree, for example if you want to override products/show.html.erb, just drop it in app/views/spree/products/show.html.erb. Be careful, your view will be rendered from the corresponding spree controller, so you may not have access to your usual helpers

  • to override a controller do the same. You can also use inheritance if you want just a little customisation...

  • to render a spree view inside one of yours just use render :file => and point out to the right path inside the gem.

that's it, the only other thing you will need is to dive a bit inside the spree gem in order to know which files you want to override / which classes you want to inherit from. A little bit of reading about engines wouldn't hurt, too.

Upvotes: 0

Ryan Bigg
Ryan Bigg

Reputation: 107738

Spree has a module called Spree::ControllerHelpers::CurrentOrder that you could include into your ApplicationController. This would make the current_order method available in all views for your application.

Upvotes: 2

Related Questions