image_tag - Is there a way to make the alt attr blank by default?

|▌冷眼眸甩不掉的悲伤 提交于 2020-01-21 08:49:06

问题


I rather have a blank alt attribute if an image is missing instead of the filename of the image (who ever wants this default behavior anyway?)

Is there a way to make a system configuration so image_tag sets a blank alt attr if no alt was given?

Just to be clear: I am not interested in image_tag('path/to/image', :alt => '') I am interested in setting it once for all the image_tags.


回答1:


For Rails 3.2 add this under config/initializers

module ActionView::Helpers::AssetTagHelper

  def image_alt(src)
    return ''
  end

end



回答2:


You can monkey-patch ActionView::Helper#image_alt with:

def image_alt(src)
  ''
end



回答3:


Or use your own version of image helper

#ApplicationHelper
def my_image_tag(source, options={})
  options(:alt) = ''
  image_tag(source, options)
end

Then use it, my_image_tag "test/test.png"



来源:https://stackoverflow.com/questions/17115771/image-tag-is-there-a-way-to-make-the-alt-attr-blank-by-default

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!