Reputation: 818
I'm using Stripe for payments on my Rails app and I've hit the error above. I've recently moved a big chunk of my code from my controller to model and this is the first time I've hit this error (I've tested payments before and it never came up). Not really sure why this is coming up now.
Here's my Model code -
Booking.rb
class Booking < ActiveRecord::Base
belongs_to :event
belongs_to :user
def reserve
# Don't process this booking if it isn't valid
return unless valid?
# We can always set this, even for free events because their price will be 0.
self.total_amount = quantity.to_i * event.price_pennies.to_i
# Free events don't need to do anything special
if event.is_free?
save
# Paid events should charge the customer's card
else
begin
charge = Stripe::Charge.create(amount: total_amount, currency: "gbp", card: @booking.stripe_token, description: "Booking number #{@booking.id}", items: [{quantity: @booking.quantity}])
self.stripe_charge_id = charge.id
save
rescue Stripe::CardError => e
errors.add(:base, e.message)
false
end
end
end
end
And in my controller -
bookings_controller.rb
def create
# actually process the booking
@event = Event.find(params[:event_id])
@booking = @event.bookings.new(booking_params)
@booking.user = current_user
if @booking.reserve
flash[:success] = "Your place on our event has been booked"
redirect_to event_path(@event)
else
flash[:error] = "Booking unsuccessful"
render "new"
end
end
Here's the error message -
I'm pretty new to Rails so apologies if this seems straightforward, any help would be appreciated.
Upvotes: 0
Views: 309
Reputation: 106
@booking
is an instance variable that is only available in the context of the controller/view. Since reserve is an instance method on the model, you probably just want to refer to self
or nothing, i.e @booking.method
=> self.method
or method
.
Upvotes: 1