没有ID找不到产品

时间:2018-12-10 15:54:39

标签: ruby-on-rails ruby ruby-on-rails-5 stripe-payments

我在PaymentsController#create错误中收到ActiveRecord :: RecordNotFound错误/找不到没有ID的产品

我正在尝试设置Stripe付款,但是在提交信用卡后出现错误。我正在为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)

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

payments / _form.html.erb

<%= form_for [@product, @payment] do |f| %>

2 个答案:

答案 0 :(得分:1)

您没有在参数中传递产品ID :product_id。理想情况下,这是路由问题。

对于创建产品付款的请求,URL的结构应为POST /products/:product_id/payments。这样,您就不必在表单中将product_id明确地提到为隐藏字段,即可将其作为请求中的参数传递。

以下是在您的情况下生成嵌套路由的示例。

resources :products do
   resources :payments
end

要使其生效,必须进行的更改的详细说明:

routes.rb中 删除resources :paymentsresources :products。这两行将替换为

resources :products do
   resources :payments
end

app/controllers/payments_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的商品,要为该商品添加付款,您可以通过以下网址访问其付款表格 /products/1/payments/new

答案 1 :(得分:0)

如果您查看控制台日志,则请求中不存在params[:product_id]

Parameters: {"utf8"=>"✓", "authenticity_token"=>"jJhGPS9Su0IuepHt2Eea/hCEhDo3A3fggu6EUjwLDrJphrC8VmNRycUqzyLGiJpcaN3mHOzr224BYsbgbjo38Q==", "stripeToken"=>"tok_1Dfr0PHrTxlTJx3aoOtOh2Z9", "stripeTokenType"=>"card", "stripeEmail"=>"myemail@gmail.com"}

那是你的问题。更改将参数发送给product_id的请求,以便您进行监视。