【问题标题】:undefined local variable or method in Ruby on Rails app OneMonth StripRuby on Rails 应用程序 OneMonth Strip 中未定义的局部变量或方法
【发布时间】:2014-09-26 18:16:21
【问题描述】:

我遇到了这个错误,我正在寻找合适的位置来查看我的代码以找出它出现的原因。我已经搜索了在哪里定义变量或方法,但还没有弄清楚。有什么建议吗?

ChargesController#create 中的名称错误 #

的未定义局部变量或方法“产品”

提取的源代码(第 13 行附近): 11 12 13 14 15 16

      charge = Stripe::Charge.create(
        :customer    => customer.id,
        :amount      => product.price_in_cents,
        :description => product.full_description,
        :currency    => 'usd'
      )

这两行是有错误的。 :amount => product.price_in_cents, :description => product.full_description,

这是我的 product.rb 模型

class Product < ActiveRecord::Base
def full_description
 "#{self.title} #{self.subtitle}"
end
def price_in_cents
 (self.price * 100).to_i
end

结束

这是我的收费控制器

class ChargesController < ApplicationController
    def create
      # Amount in cents
      @amount = 500

      customer = Stripe::Customer.create(
        :email => params[:stripeEmail],
        :card  => params[:stripeToken]
      )

      charge = Stripe::Charge.create(
        :customer    => customer.id,
        :amount      => product.price_in_cents,
        :description => product.full_description,
        :currency    => 'usd'
      )

     purchase = Purchase.create(email: params[:stripeEmail], card: params[:stripeToken], 
        amount: product.price_in_cents, description: charge.description, currency: charge.currency,
        customer_id: customer.id, product_id: product.id, uuid: SecureRandom.uuid)

        redirect_to purchase

    rescue Stripe::CardError => e
      flash[:error] = e.message
      redirect_to charges_path
    end

结束

【问题讨论】:

    标签: ruby-on-rails


    【解决方案1】:

    您在 create 方法中引用了 product 变量,但您尚未创建它。

    你可能需要类似的东西

    product = Product.find(params[:product_id])
    

    【讨论】:

    • 谢谢!那很接近,但它让我走上了正确的轨道。我在另一个控制器中定义了产品,需要添加产品的跟踪方式 product = Product.find_by_sku("GROHACK1")
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多