问题
On Rails 4.1 .. being new to Spree 2-3-stable and experimenting with various things. My test store works fine and now I would am trying and integrate it into my application which is a booking site.
On my Bookings calendar I would like users to click on a booking link and have that auto populate their cart, then show their cart directly.
So assuming I have a spree product, and a variant existing in the database, and the current_user is logged in (ie spree_current_user exists) .. I thought this would be possible but it seems it will not even create the populator.
class BookingsController < ApplicationController
def add_booking_to_cart
populator = Spree::OrderPopulator.new Spree::Order.current_order(create_order_if_necessary: true), current_currency
# .. get variant related to booking
# .. add varient to populator
end
#... rest of bookings controller
end
Errors out with:
NoMethodError - undefined method `current_order' for #<Class:0x00000007d4fc68>:
I am attempting to do a similar thing to what Will is doing here but am getting the error undefined method on current_order object. My understanding of the code is that it should create a new order if necessary, ie if one does not exist. No sure what do to here? Any pointer would be greatly aprecieated.
https://github.com/spree/spree/blob/2-3-stable/frontend/app/controllers/spree/orders_controller.rb#L46
回答1:
For this to work your controller needs to extend Spree::StoreController
class BookingController < Spree::StoreController
This is where the method is defined in Spree: https://github.com/spree/spree/blob/master/core/lib/spree/core/controller_helpers/order.rb
Also, you might need to have a before_filter :set_current_order
.
来源:https://stackoverflow.com/questions/24759046/spreeorderpopulator-accessing-outside-of-store