How to pass wrong value as argument in I18n using rails validates inclusion validation?

Viewed 115

I want to validate inclusion in country_code field in my Country table. And I want to show specific error for the invalid record using I18n.

My model:

class Country < ApplicationRecord
  validates :country_code,
        inclusion: { in: ISO3166::Country.codes, allow_blank: true, message: I18n.t('models.country.invalid_country_code', value: value) }

end

My localisation:

de:
  models:
    country:
      invalid_country_code: "%{value} is invalid"

But I see NameError: undefined local variable or method "value" for #<Class:0x00007f8e5de83dc8> error message in the console.

1 Answers

I found the solution: putting value in quotes with % fix this problem.

class Country < ApplicationRecord
  validates :country_code,
    inclusion: { in: ISO3166::Country.codes, allow_blank: true, message: I18n.t('models.country.invalid_country_code', value: '%{value}') }

end
Related