Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

302
Vistas
difference between %{count} and %<count>s in validation message

I am used to the approach below when displaying a validation message

class Person < ApplicationRecord
  validates :bio, length: { maximum: 1000,
    too_long: "%{count} characters is the maximum allowed" }
end

However, I just found out that below is also possible. But I could not find any documentation on %<>s format. And what is the use of s found at the end?

    too_long: "%<count>s characters is the maximum allowed" }

Can someone explain the difference between these syntaxes? Or provide a documentation link on %<>s

reference:
https://guides.rubyonrails.org/active_record_validations.html#length

over 4 years ago · Santiago Trujillo
2 Respuestas
Responde la pregunta

0

That s in %<...>s denotes "string". From the sprintf docs:

For more complex formatting, Ruby supports a reference by name. %<name>s style uses format style, but %{name} style doesn't.

Example:

sprintf('%<count>s characters', count: '2,300')
=> "2,300 characters"
over 4 years ago · Santiago Trujillo Denunciar

0

Rails uses I18n internally to interpolate error messages. So for answers we should refer to its documentation. Or its source - the latter explains (hopefully) why %<>s pattern works for you.

But honestly I would avoid using non-documented features. If Rails' team decides to change the error messages interpolation machinery one day your code becomes broken - just because you relied on the internals and their non-documented features. This is unlikely to happen in this particular case, but still...

over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda