mercredi 22 juin 2016

ActiveRecord::RecordNotFound in ChargesController#create

I have an app with a user model, pin model, and charges controller for stripe payments. I get the following error when I login in as a user in development and try to purchase a pin via stripe payments:

ActiveRecord::RecordNotFound in ChargesController#create

Couldn't find Pin without an ID

It indicates that the error is coming from @pin = Pin.find(params[:id]) from the code below. I believe it is doing this because the pin is still 'nil. I am not sure if or how I would go about assigning a value to it so that it corresponds to the exact price associated with that pin.

class ChargesController < ApplicationController

    def create
      # Amount in cents
      @pin = Pin.find(params[:id])
      @amount = (@pin.price * 100).floor

      customer = Stripe::Customer.create(
        :email => params[:stripeEmail],
        :card  => 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 charges_path
    end

end

Also, whatever button or link calls the ChargesController#create method is likely in the code below here in the pins show.html.erb:

app/views/pins/show/html.erb

      <% if @pin.user == current_user %>

          <%= form_tag charges_path, id: 'chargesForm' do %>
              <script src="http://ift.tt/1doUtf9"></script>
              <%= hidden_field_tag 'stripeToken' %>
              <%= hidden_field_tag 'stripeEmail' %>  
              <button id="btn-buy" type="button" class="btn btn-success btn-lg btn-block"><span class="glyphicon glyphicon-heart"></span>   I want this!</button>

              <script>
                  var handler = StripeCheckout.configure({
                    key: '<%= Rails.configuration.stripe[:publishable_key] %>',
                    token: function(token, arg) {
                      document.getElementById("stripeToken").value = token.id;
                      document.getElementById("stripeEmail").value = token.email;
                      document.getElementById("chargesForm").submit();
                    }
                  });
                   document.getElementById('btn-buy').addEventListener('click', function(e) {
                    handler.open({
                      name: <%= @pin.manufacturer %>',
                      description: '<%= @pin.description %>',
                      amount: '<%= (@pin.price * 100).floor %>'
                  });
                  e.preventDefault();
                 })
              </script>
          <% end %>

      <% else %>

          <%= link_to 'I want this!', new_user_registration_path, class: "btn btn-success btn-lg btn-block" %>

      <% end %>

   <ul id="details-infobox" class="list-group">
          <li class="list-group-item active clearfix">DETAILS</li>
          <li class="list-group-item">
            <p><strong>Description:</strong>   <%= @pin.description %></p></li>
          <li class="list-group-item clearfix">
            <span class="pull-left content-qualifier"><b>Price: $ <%= @pin.price %></b></span>

          </li>
        </ul>
    </aside>

</section>

What should I do to patch this issue? Thanks!

Aucun commentaire:

Enregistrer un commentaire