尝试更新用户表中的 customer_id 列时出现此错误,但不知道原因。用户模型 has_many :orders 和 Order 模型 belongs_to :user,orders 表具有正确的 users_id 外键,其他依赖这种关系的东西工作正常。
错误:
undefined local variable or method `customer_id' for #<Order:0x007ff27014f9f0>
app/models/order.rb:16:in `save_with_payment'
app/controllers/orders_controller.rb:53:in `block in create'
app/controllers/orders_controller.rb:52:in `create'
订单.rb
belongs_to :user
def save_with_payment
if valid?
customer = Stripe::Customer.create(description: email, card: stripe_card_token)
self.stripe_customer_token = customer.id
self.user.update_column(customer_id, customer.id) # this is line 16
save!
Stripe::Charge.create(
:amount => (total * 100).to_i, # in cents
:currency => "usd",
:customer => customer.id
)
end
rescue Stripe::InvalidRequestError => e
logger.error "Stripe error while creating customer: #{e.message}"
errors.add :base, "There was a problem with your credit card."
false
end
订单控制器.rb
def create
if current_user
@order = current_user.orders.new(params[:order])
else
@order = Order.new(params[:order])
end
respond_to do |format| # this is line 52
if @order.save_with_payment # this is line 53
format.html { redirect_to auctions_path, :notice => 'Your payment has been successfully processed and your credit card has been linked to your account.' }
format.json { render json: @order, status: :created, location: @order }
format.js
else
format.html { render action: "new" }
format.json { render json: @order.errors, status: :unprocessable_entity }
format.js
end
end
end
用户.rb
has_many :orders