本文介绍了尝试使用实例化对象调用函数时没有方法错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个模型令牌,其中包含三个字段 user_id,product_idunique_token.在控制器中,我使用 @token 实例化了一个对象>user_id 和 product_id 值 从表单中收集.然后我使用该对象调用 save_with_payment 函数,在该函数中我想生成随机字符串 3 次并保存在 unique_token 字段中.问题是self.tokens.create!( unique_token: Digest::SHA1.hexdigest("random string") ) 给我没有方法错误未定义的方法令牌.我在这里做错了什么?为了澄清我想要完成的任务,我希望能够检索与该 user_idproduct_id 相关联的生成的 unique_tokens 列表,例如User.find(1).tokensProduct.find(1).tokens.模型关联为 User has_many Tokens Producthas_many Tokens.注意:unique_token 字段最初来自 Token 模型,user_id 和 product_id 只是 ref 主键.非常感谢!

I have a model Token with three fields user_id,product_id and unique_token.In the controller i instantiate a @token object with user_id and product_id values collected from the form.Then i call save_with_payment function with that object,where within the function i want to generate random string 3 times and save in unique_token field.The problem is self.tokens.create!( unique_token: Digest::SHA1.hexdigest("random string") ) give me no method error undefined method tokens.What am i doing wrong here?To clarify what i want to accomplish,I want to be able to retrieve list of generated unique_tokens associated to that user_id or product_id like User.find(1).tokens or Product.find(1).tokens.The model association is User has_many Tokens Product has_many Tokens.Note: unique_token field is from Token model originally,user_id and product_id are just ref primary keys.Much Thanks!

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

推荐答案

Token 类没有标记方法.由于您正在创建三个令牌,因此您不需要 @token 实例.只需让 save_with_payment 成为一个类方法:

There is no tokens method on the Token class. Since you're creating three tokens you don't need the @token instance. Just have save_with_payment be a class method:

def create
  if Token.save_with_payment(params[:token])
    redirect_to :controller => "products", :action => "index"
  else
    redirect_to :action => "new"
  end
end

class Token < ActiveRecord::Base
  require 'digest/sha1'

  def self.save_with_payment(attributes)
    attributes.merge!(unique_token: Digest::SHA1.hexdigest("foo"))
    3.times do
      self.create!(attributes)
    end
  end

end

希望这会有所帮助.

您可能还想将循环包装在开始/救援中.否则,如果第 2 次或第 3 次创建!失败,你最终得到令牌并重定向到新".

You might want to wrap the loop in a begin/rescue, too. Otherwise if the 2nd or 3 create! fails you end up with tokens AND redirecting to "new".

对第一条评论的回应:如果您使用类方法,那将不起作用.你不能叫valid?因为您不在 Token 实例的上下文中.我不建议坚持使用实例方法.如果确实将其更改为类方法,则需要将其包装在事务块中:

Response to 1st comment:That won't work if you use a class method. You can't call valid? because you're not in the context of an instance of Token. I don't recommend sticking with an instance method. If you do change it to a class method you'll want to wrap it in a transaction block:

def self.save_with_payment(attributes)
  transaction do
    attributes.merge!(unique_token: Digest::SHA1.hexdigest("foo"))
    3.times do
      self.create!(attributes)
    end
  rescue
    false
  end
end

如果有任何创建,那应该回滚 SQL 事务!调用失败并返回 false 给控制器创建操作.

That should roll back the SQL transactions if any of the create! calls fail and return false to the controller create action.

我会从 Token 中提取该客户代码(Token 不应该关心创建/检索客户)并将其放入控制器操作中.将相关信息传递到 save_with_payments 中.喜欢:

I'd pull that customer code out of Token (Token shouldn't care about creating/retrieving a customer) and put it in the controller action. Pass the pertinent information into save_with_payments. Like:

self.save_with_payments(customer, attributes)
  ...
end

这篇关于尝试使用实例化对象调用函数时没有方法错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-23 06:18