I use Rails 4 with the Rails-i18n Gem and i want replace my hard coded string "300px" with a placeholder in my language translation file like %{minimum_resolution} in config/locales/de.yml
activerecord:
errors:
models:
organisation:
attributes:
image:
resolution_too_small:"Image Resolution should be at least %{minimum_resolution}"
The value in %{minimum_resolution} should come from my custom validation in app/models/organisation.rb
def validate_minimum_image_dimensions
if image.present?
logo = MiniMagick::Image.open(image.path)
minimum_resolution = 300
unless logo[:width] > minimum_resolution || logo[:height] > minimum_resolution
errors.add :image, :minimum_image_size
end
else
return false
end
end
How can i get the value from minimum_resolution into my yaml file?
Try this, and let me know
def validate_minimum_image_dimensions
if image.present?
logo = MiniMagick::Image.open(image.path)
minimum_resolution = 300
unless logo[:width] > minimum_resolution || logo[:height] > minimum_resolution
errors.add :image, :resolution_too_small, minimum_resolution: minimum_resolution
end
else
return false
end
end
Anyway, this is the syntax
errors.add :field_name, :message_key, {optional_param1: value1, optional_param2: value2}
and it has to be defined like this
activerecord:
errors:
models:
[your_model]:
attributes:
[field_name]:
[message_key]: "Image Resolution should be at least %{optional_param1} and %{optional_param2}"