我有三个字段USER_ID模型令牌时,没有方法的错误,PRODUCT_ID和unique_token。在控制器我实例化一个@token对象与user_id说明和的product_id值从form.Then收集我打电话save_with_payment功能与该对象,函数内我要生成随机字符串的3倍,并保存在unique_token场。问题是self.tokens.create!(unique_token: Digest::SHA1.hexdigest("random string"))
给我都没法错误undefined method tokens
。什么我在这里做错了吗?澄清你T I要完成,我希望能够检索关联到USER_ID或PRODUCT_ID像User.find(1).tokens
或Product.find(1).tokens
.The模式相伴随而产生unique_tokens的名单User has_many Tokens
Product has_many Tokens
。 注意:unique_token字段最初来自Token模型,user_id和product_id只是ref主键。很感谢!试图调用函数实例化对象
def create
@token=Token.new(params[:token])
if @token.save_with_payment
redirect_to :controller => "products", :action => "index"
else
redirect_to :action => "new"
end
end
class Token < ActiveRecord::Base
require 'digest/sha1'
def save_with_payment
# if valid?
# customer = Stripe::Charge.create(amount:buck,:currency => "usd",card:stripe_card_token,:description => "Charge for bucks")
#self.stripe_customer_token = customer.id
3.times do
self.tokens.create!(unique_token: Digest::SHA1.hexdigest("random string"))
end
save!
end
end
这似乎是有益的,但它的代码我以前那种intefere,如果你看一下上面我已经加入注释掉的代码,我不觉得有必要提前加,所以现在说未定义方法是否有效?。buck和stripe_card_token的值也来自表单(Token.new(params [; token]))。现在如何改变它? – katie 2012-01-27 23:20:22