Я получаю ActiveRecord :: RecordNotFound в PaymentsController # создать ошибку / Не могу найти продукт без идентификатора
Я пытаюсь настроить платежи Stripe, но получаю ошибку после получения кредитакарта подана.Я делаю это для класса от Intro до Rails, и мои одноклассники имеют тот же код, но их «работает».
Похоже, что ошибка в этой строке:
@product = Product.find(params[:product_id])
Вот мой контроллер платежей:
class PaymentsController < ApplicationController
def new
@product = @product.payments.new
end
def create
token = params[:stripeToken]
@product = Product.find(params[:product_id])
@user = current_user
# Create the charge on Stripe's servers - this will charge the user's card
begin
charge = Stripe::Charge.create(
amount: (@product.price*100), # amount in cents, again
currency: "usd",
source: token,
description: params[:stripeEmail]
)
if charge.paid
Order.create(
product_id: @product.id,
user_id: @user.id,
total: @product.price
)
#flash[:success] = "You have successfully paid for your order!"
UserMailer.order_confirmation_email(@user, @product).deliver_now
end
rescue Stripe::CardError => e
# The card has been declined
body = e.json_body
err = body[:error]
flash[:error] = "Unfortunately, there was an error processing your payment: #{err[:message]}"
end
redirect_to product_path(@product), notice: "Thank you for your purchase."
end
end
Вот консольное сообщение:
Started POST "/payments/create" for 127.0.0.1 at 2018-12-10 10:49:15 -0500
Processing by PaymentsController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"jJhGPS9Su0IuepHt2Eea/hCEhDo3A3fggu6EUjwLDrJphrC8VmNRycUqzyLGiJpcaN3mHOzr224BYsbgbjo38Q==", "stripeToken"=>"tok_1Dfr0PHrTxlTJx3aoOtOh2Z9", "stripeTokenType"=>"card", "stripeEmail"=>"myemail@gmail.com"}
Completed 404 Not Found in 2ms (ActiveRecord: 0.0ms)
rout.rb
Rails.application.routes.draw do
devise_for :users, path: '', path_names: { sign_in: 'login', sign_out: 'logout' }
resources :users
resources :products, :invoices, :orders, :users # 5.1 added ", :invoices, :orders, :users"
resources :users, except: [:index]
get 'simple_pages/about'
get 'simple_pages/contact'
get 'simple_pages/index'
get 'simple_pages/landing_page'
post 'simple_pages/thank_you'
post 'payments/create'
root 'simple_pages#landing_page'
mount ActionCable.server => '/cable'
resources :products do # 5.8 added
resources :comments
end
resources :products do
resources :payments
end
# For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
end
payment / _form.html.erb
<%= form_for [@product, @payment] do |f| %>