Rails 5.2 deprecated the Action View helper image_alt(src).
The method takes the src
argument, which can be an image file path. It removes the basename and the digest of the image file path, and returns a titleized string after replacing hyphens and underscores with spaces.
Let’s see an example:
Usage in image_tag
Before deprecation, image_alt
was used to autogenerate alt
text from the src
attribute of an image_tag
.
Example:
Adding the following in a view:
Would render:
Problems with default alt text
In the example above, the alt
text “Brand logo” will be read by screen readers.
More often than not, this automatically inferred text is not a useful description and adds unnecessary content.
Setting a meaningless alt
text messes with the screen reader’s default behavior for blank alt
text.
That may be frustrating for assistive technology users.
This also results in linting tools giving false negatives.
It also makes improving application accessibility a tough job.
Rails 5.2 onwards
After deprecation, we had to explicitly set the alt
text while using an image_tag
.
And also had the option to just not provide an alt
text.
The generated image
tag would have no alt
text in that case.
Example:
Would render:
Rails 6.0
In Rails 6.0, this method has been removed. This was done as a part of removing all code which was deprecated in Rails 5.2.