Reputation: 4648
I am trying to update my inventory after a Stripe transaction
routes:
get 'product/:size' => 'charges#new'
resources :charges
ex: localhost:3000/product/32
controller
def new
@product = Product.find_by(size: params[:size])
end
def create
@amount = 500
@product = Product.find_by(size: params[:size])
customer = Stripe::Customer.create(
:email => '[email protected]',
:card => params[:stripeToken]
)
charge = Stripe::Charge.create(
:customer => customer.id,
:amount => @amount,
:description => 'Rails Stripe customer',
:currency => 'usd'
)
@product.update_attribute(:status, "sold")
rescue Stripe::CardError => e
flash[:error] = e.message
redirect_to charges_path
end
end
This returns
undefined method `update_attribute' for nil:NilClass
After testing through trial and error, I believe that the problem is that Rails is not finding [:size] in params, thus the instance variable is not being instantiated.
What must I do?
Upvotes: 0
Views: 74
Reputation: 24337
Make sure to include the :size param into your form so that it is passed along to the create action during the POST. You can include it in your form using hidden_field_tag
:
<%= form ... do %>
<%= hidden_field_tag :size, params[:size] %>
Other form inputs....
<% end %>
Upvotes: 1