Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

>Solution :

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"
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading