views:

72

answers:

1

I followed the railscast on activemerchant and have this code:

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

But that doesn't wrap the field in a fieldWithErrors div. So I tried:

def validate_card
  unless credit_card.valid?
    credit_card.errors.each do |error|
      errors.add error
    end
  end
end

That still didn't work. I've read http://api.rubyonrails.org/classes/ActiveResource/Errors.html and http://activemerchant.rubyforge.org/ but I'm not reading them right or something.

+1  A: 

Adding it to the list of errors and showing that error(s) later are two different things.

The first way you've got it looks right. That puts the error into the list of errors for this object (there might be other validation errors, for instance.)

Then, you can use error_messages_for() (api ref) to output that error in your view. It's customizable for whatever you want to call your divs.

Or, you can do your own output by looping through @object.errors yourself.

wesgarrison