Erik V
Erik V

Reputation: 375

Erroneous "No route matches" error Rails

This is my first time integrating stripe. I have set pages#purchase as my homepage. I am using a payments_controller to handle the stripe payment. Error

ActionController::RoutingError (No route matches [POST] "/payments/create"):

Routes

resources :payments, only: [:create]

Rake Routes

payments POST   /payments(.:format) payments#create

Pages/Purchase

<%= form_tag "/payments/create" do %>
  <%= render partial: "shared/stripe_checkout_button" %>
<% end %>

Shared/_stripe_checkout_button

<script
  src="https://checkout.stripe.com/checkout.js" class="stripe-button"
  data-key="<%= Rails.configuration.stripe[:publishable_key] %>"
  data-image="/square-image.png"
  data-name="Demo Site"
  data-description="2 widgets ($20.00)"
  data-amount="2000">
</script>

Payments Controller

def create #You want might to make actions more specific.
    token = params[:stripeToken] #The token when the form is posted includes the stripe token in the url.
    # Create the charge in stripes servers.  This is what commits the transaction.
    begin
      charge = Stripe::Charge.create(
        :amount => 200,
        :currency => "usd",
        :source => token,
        :description => params[:stripeEmail]
        )
      rescue Stripe::CardError => e 
        #The card was decline because of number/ccv error, expired error, bank decline, or zip error

        body = e.json_body
        err = body[:error]
        flash[:error] = "There was an error in processing your card: #{err[:message]}"
    end
  end

Upvotes: 2

Views: 434

Answers (1)

Arup Rakshit
Arup Rakshit

Reputation: 118271

Your form_tag url is wrong. It must be :

<%= form_tag "/payments" do %>
  <%= render partial: "shared/stripe_checkout_button" %>
<% end %>

The :method for the form defaults to POST.

Upvotes: 4

Related Questions