0

I am using Stripe Checkout and I would like to the user to set how much he/she wants to pay. I would like to pass variables from the "new" to "create" controller, specifically the donation amount. How do I pass the @amount variable from "new" to "create" using the Stripe template. In my controller I have:

def new
    @amount = 10
    @amount_in_cents = @amount * 100
end

def create
  # Amount in cents...HOW DO I GET THIS TO BE PULLED FROM 'NEW'
  @amount = 500

  customer = Stripe::Customer.create(
    :email => params[:stripeEmail],
    :source  => params[:stripeToken]
  )

  charge = Stripe::Charge.create(
    :customer    => customer.id,
    :amount      => @amount,
    :description => 'Rails Stripe customer',
    :currency    => 'usd'
  )

rescue Stripe::CardError => e
  flash[:error] = e.message
  redirect_to new_charge_path
end

My view for new.html.erb:

    <%= form_tag charges_path do %>
      <article>
        <% if flash[:error].present? %>
          <div id="error_explanation">
            <p><%= flash[:error] %></p>
          </div>
        <% end %>
        <label class="amount">
          <span>Amount: $<%= @amount %></span>
        </label>
      </article>

      <script src="https://checkout.stripe.com/checkout.js" class="stripe-button"
              data-key="<%= Rails.configuration.stripe[:publishable_key] %>"
              data-description="Your donation of $<%= @amount %>"
              data-amount="<%= @amount_in_cents %>"
              data-email = "test@gmail.com"
              data-locale="auto"></script>
    <% end %>

View for create.html.erb

    <h2>Thanks, you paid <strong>$5.00</strong>!</h2>

    <%= @amount %>
sharataka
  • 4,922
  • 20
  • 64
  • 119
  • What's the problem? You're already passing it from `#new` to your view and to `#create`. – EJAg Oct 03 '17 at 05:36

1 Answers1

0

While you are using @ sign before variable name, you are making that variable available to instance of that class. So your @amount var will be available in your class and your view.

class Transaction 

 def new 
  @amount=12
 end

 def create

  puts @amount+13
  @amount=@amount+12

 end

end

me = Transaction.new 
puts me.new 
puts me.create

ruby variable scope

instance variables in ruby

omurbek
  • 562
  • 1
  • 5
  • 21