Michael Lombardo Michael Lombardo - 4 months ago 14
Ruby Question

undefined method `total' for nil:NilClass NoMethodError in ReservationsController#checkout

Im currently stuck unable to figure out how to get params from one controller to add to the next controller. Iv tried

render
and
redirect_to
and I still cant get this to work!!

Heres my controller and view
- form in view gets the action of my
checkout controller

-I want to use the same reservation.id in both checkout and create controllers, so I can call reservation.total in the checkout controller.

How can I get the total on create controller to hope over and share on the checkout controller??

Error Log:

NoMethodError (undefined method `total' for nil:NilClass):
app/controllers/reservations_controller.rb:45:in `checkout'


reservations controller:

def checkout




nonce = params[:payment_method_nonce]
result = Braintree::Transaction.sale(
:amount => @reservation.total,
:payment_method_nonce => nonce,
:options => {
:submit_for_settlement => true
}
)
if result.success?
puts "success!: #{result.transaction.id}"
elsif result.transaction
puts "Error processing transaction:"
puts "code: #{result.transaction.processor_response_code}"
puts "text: #{result.transaction.processor_response_text}"
else
p result.errors
end


end



def create

#!/usr/bin/env ruby


reviser = Reviser.find(params[:reviser_id])
@reservation = current_user.reservations.create(reservation_params)

if @reservation.total > 0

@token = Braintree::ClientToken.generate


elsif @reservation.total == 0
@reservation.update_attributes status: true
redirect_to @reservation.reviser.user, alert: "Writing Request Success"


else
redirect_to @reservation.reviser, alert: "Oops, something went wrong..."
end




end


create html:



<p>create</p>


<form id="checkout" method="post" action="/checkout">
<div id="payment-form"></div>
<input type="submit" value="Pay">
</form>

<script src="https://js.braintreegateway.com/js/braintree-2.26.0.min.js"></script>
<script>
// We generated a client token for you so you can test out this code
// immediately. In a production-ready integration, you will need to
// generate a client token on your server (see section below).
// var clientToken = "eyJ2ZXJzaW9uIjoyLCJhdXRob3JpemF0aW9uRmluZ2VycHJpbnQiOiI5OGRjMDcyZDFmM2IwNzdlNmI2YmE1NTQ0ZDIxYzQ4YjliZmZlZDA3YmZhNTgxYzRkMTU4MzU2MWIyMjIxYzJlfGNyZWF0ZWRfYXQ9MjAxNi0wNy0xNVQwNjo0OTo0MS4zMTczNjM3NDArMDAwMFx1MDAyNm1lcmN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kaXJlY3RCYXNlVXJsIjpudWxsLCJhbGxvd0h0dHAiOnRydWUsImVudmlyb25tZW50Tm9OZXR3b3JrIjp0cnVlLCJlbnZpcm9ubWVudCI6Im9mZmxpbmUiLCJ1bnZldHRlZE1lcmNoYW50IjpmYWxzZSwiYnJhaW50cmVlQ2xpZW50SWQiOiJtYXN0ZXJjbGllbnQzIiwiYmlsbGluZ0FncmVlbWVudHNFbmFibGVkIjp0cnVlLCJtZXJjaGFudEFjY291bnRJZCI6ImFjbWV3aWRnZXRzbHRkc2FuZGJveCIsImN1cnJlbmN5SXNvQ29kZSI6IlVTRCJ9LCJjb2luYmFzZUVuYWJsZWQiOmZhbHNlLCJtZXJjaGFudElkIjoiMzQ4cGs5Y2dmM2JneXcyYiIsInZlbm1vIjoib2ZmIn0=";



var clientToken = "<%= @token %>";

braintree.setup(clientToken, "dropin", {
container: "payment-form"
});
</script>





post 'checkout', to: 'reservations#checkout'
get 'checkout', to: 'reservations#checkout'

Answer

I will suggest you to make the checkout method as a member method i.e pass the id of the reservation in the url.

post 'checkout/:id', to: 'reservations#checkout', :as => 'reservation_checkout'

By doing this you will be able to find the reservation like.

@reservation = Reservation.find(params[:id]) #so you will not get the error undefined method `total' for nil:NilClass

I am wondering why don't you use html.erb

<%= form_tag reservation_checkout_path(@reservation) %>
  #your fields
<% end %>
Comments