Ruby on rails 不能';找不到没有ID的产品
我收到一个ActiveRecord::RecordNotFound in PaymentsController#create error/找不到没有ID的产品 我正在尝试设置条带支付,但在提交信用卡后出现错误。我在做Rails入门课程的介绍,我的同学们都有相同的代码,但他们的代码仍然有效 错误似乎在这一行:Ruby on rails 不能';找不到没有ID的产品,ruby-on-rails,ruby,ruby-on-rails-5,stripe-payments,Ruby On Rails,Ruby,Ruby On Rails 5,Stripe Payments,我收到一个ActiveRecord::RecordNotFound in PaymentsController#create error/找不到没有ID的产品 我正在尝试设置条带支付,但在提交信用卡后出现错误。我在做Rails入门课程的介绍,我的同学们都有相同的代码,但他们的代码仍然有效 错误似乎在这一行: @product = Product.find(params[:product_id]) 这是我的付款管理员: class PaymentsController < Applicat
@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
routes.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
付款/_form.html.erb
<%= form_for [@product, @payment] do |f| %>
如果您在控制台日志中检查,
参数[:product\u id]
在请求中不存在
Parameters: {"utf8"=>"✓", "authenticity_token"=>"jJhGPS9Su0IuepHt2Eea/hCEhDo3A3fggu6EUjwLDrJphrC8VmNRycUqzyLGiJpcaN3mHOzr224BYsbgbjo38Q==", "stripeToken"=>"tok_1Dfr0PHrTxlTJx3aoOtOh2Z9", "stripeTokenType"=>"card", "stripeEmail"=>"myemail@gmail.com"}
这是你的问题。更改请求以在参数上发送产品id,以便您的了望功能正常。您没有在参数中传递产品id
:产品id
。理想情况下,这是一个路由问题
对于为产品创建付款的请求,URL的结构应该是POST/products/:product\u id/payments
。这样,您就不必在表单中将product\u id
作为隐藏字段显式提及,以便在请求中将其作为参数传递
以下是在您的案例中生成嵌套管线的示例
resources :products do
resources :payments
end
详细说明为使其正常工作而必须进行的更改:
在routes.rb中
删除资源:付款
和资源:产品
。这两条线将被替换为
resources :products do
resources :payments
end
在app/controllers/payments\u controller.rb中
def new
@product = Product.find(params[:product_id])
@payment = @product.payments.new
end
在app/views/payments/_form.html.erb
<%= form_for [@product, @payment] do |f| %>
假设您有一个ID为1的产品,为了为该产品添加付款,您可以通过URL访问其付款表单
/products/1/payments/new
请将控制台输出添加到问题中。具体来说,params[:product\u id]
的值是多少?(请编辑您的问题,不要在注释中用代码回答。)您能告诉我如何将该值输出到Rails控制台吗?在Rails控制台(Rails服务器正在运行的地方)中,您应该会看到一个以Started POST
开头的事务。它应该包括一行开头为参数:
。把整个问题都贴在你的问题上,谢谢。我已经添加了readoutThank。我不确定如何更改请求以发送参数上的产品id。我可以编辑@product=行吗?谢谢你的回复。我仍然不知道如何编辑我的代码。我对这一点很陌生,但我正试图弄明白。我已经用一个例子很好地解释了使用嵌套资源的表单实现。我已经通读了这些代码,并将您的示例添加到了我的路由中,但我真的不知道还需要做些什么才能使其正常工作。很抱歉,我对此非常缺乏经验,只想让它工作。请您分享更改代码后您面临的错误,并分享您的routes.rb
文件好吗?不必担心经验不足,我们都从第一步开始。谢谢,标题中仍然存在相同的错误:ActiveRecord::RecordNotFound in PaymentsController#create,突出显示@product line。要明确的是,我没有在Payments Controller中编辑任何内容(我真的不确定要编辑什么)
<%= form_for [@product, @payment] do |f| %>