Jon K.
Jon K.

Reputation: 3

Stripe: ActionController::UrlGenerationError | No route matches | missing required keys: [:id]

I am just learning RoR and need I am having trouble with Stripe integration. I have done everything as it says here except I have changed "charges" to "charge."

The error I am receiving is: No route matches {:action=>"show", :controller=>"charge"} missing required keys: [:id].

It's not letting me do: <%= form_tag charge_path do %>

This is my controller:

class ChargeController < ApplicationController
def new
end

def create

# Amount in cents
@amount = 0

customer = Stripe::Customer.create(
:email => '[email protected]',
:card  => params[:stripeToken]
)

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

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

My routes.rb file has: resources :charge

Thank you for your help!

Upvotes: 0

Views: 5181

Answers (2)

vee
vee

Reputation: 38645

You should not divert from Rails standards, that will most of the time punish you for it. You should rename your controller back to ChargesController and take a look at "Singular Resources" on how you can solve your issue.

So the changes that you need to fix your issues are as follows:

  1. Rename app/controllers/charge_controller.rb to app/controllers/charges_controller.rb
  2. Rename class ChargeController to class ChargesController
  3. Replace resources :charge with resource :charge

By replaceing resources :charge with resource :charge you will be creating a singular charge resource with the path /charge.

With your current setup i.e. resources :charge you will see the following (which is not what you want):

charge_index   GET    /charge(.:format)              charge#index
               POST   /charge(.:format)              charge#create
new_charge     GET    /charge/new(.:format)          charge#new
edit_charge    GET    /charge/:id/edit(.:format)     charge#edit
charge         GET    /charge/:id(.:format)          charge#show
               PUT    /charge/:id(.:format)          charge#update
               DELETE /charge/:id(.:format)          charge#destroy

As you can see above the charge_path resolves to charge#show but if you look at the path it also requires an :id parameter which you are not supplying in your form_tag :charge_path call.

Upvotes: 3

Finbarr
Finbarr

Reputation: 32126

You need to change your controller name to ChargesController, change the path helper to charges_path and the routes to resources :charges.

Also, the form should be POSTing.

Upvotes: 0

Related Questions