Undefined method add_to_base

那年仲夏 提交于 2019-12-20 10:05:55

问题


I'm working with activemerchant and it raise me this error when validating the card is this ok in rails 3? thank you in advance more power to all

belongs_to :reservation

  attr_accessor :card_number, :card_verification

  validate :validate_card, :on => :create

  def validate_card
    unless credit_card.valid?
      credit_card.errors.full_messages.each do |message|
        errors.add_to_base "error"
      end
    end
  end

    def credit_card
    @credit_card ||= ActiveMerchant::Billing::CreditCard.new(
      :type               => card_type,
      :number             => card_number,
      :verification_value => card_verification,
      :month              => card_expires_on.month,
      :year               => card_expires_on.year,
      :first_name         => first_name,
      :last_name          => last_name
    )
  end

it is pointing to Undefined method add_to_base


回答1:


add_to_base method was removed from rails 3. You should use errors[:base] << "error" instead.




回答2:


I prefer the following, over the accepted answer:

errors.add :base, 'error message'




回答3:


In your model just make:

:add_to_base=> false

Access it in your controller as:

model_instance.errors.messages


来源:https://stackoverflow.com/questions/10284286/undefined-method-add-to-base

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!