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

蹲街弑〆低调 提交于 2019-12-25 02:12:32

问题


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 => 'jon@jonkhaykin.com',
: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!


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/20450541/stripe-actioncontrollerurlgenerationerror-no-route-matches-missing-requir

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!