TransactionsController#new中的ActiveRecord :: RecordNotFound

时间:2015-01-26 14:16:20

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

我是一个全新的学习如何思考的问题,并且遇到了一个问题,我似乎无法在跟踪整合条纹的rails教程时弄清楚。就像我提到的那样,我仍然是学习如何思考的新手,而且似乎无法弄清楚自从我按照教程学习后到底是什么,尽管不是复制和粘贴而是阅读,寻找并编写代码来构建肌肉记忆。也许是拼写错误,但是,我要仔细检查。谢谢你的时间。

    class TransactionsController < ApplicationController
     skip_before_action :authenticate_user!, only: [:new, :create]

     def new
      @product = Product.find_by!(
       permalink: params[:permalink]
      )
    end

    def pickup
     @sale = Sale.find_by?(guid: params[:guid])
     @product = @sale.product
    end

   def create
    @product = Product.find_by!(
     permalink: params[:permalink]
   )

   token = parmas[:stripeToken]

   begin
     charge = Stripe::Charge.create(
       amount:      product.price,
       currency:    "usd",
       card:        token,
       description: params[:stipeEmail]
     )
     @sale = product.sales.create!(
       email:       params[:stripeEmail],
       stripe_id:   charge.id
     )
    redirect_to pickup_url(guid: @sale.guid)
     rescue Stripe::CardError => e
     # the card has been declined or
     # some other error has occured
     @error = e
     render :new
    end
 end

 def download
  @sale = Sale.find_by!(guid: params[:guid])

  resp = HTTParty.get(@sale.product.file.url)

  filename = @sale.product.file.url
  send_data resp.body,
   :filename => File.basename(filename),
   :content_type => resp.headers['Content-Type']
 end
end

1 个答案:

答案 0 :(得分:0)

错误出现在这段代码中

def new
  @product = Product.find_by!(
       permalink: params[:permalink]
  )
end

您在数据库中搜索Product等于permalink的{​​{1}},但记录不存在且会引发错误。您可以使用params[:permalink]而不find它不会抛出错误,但!@product

您是否真的需要在数据库中查找记录,或者需要创建新的类实例。您可以像这样使用它:

nil