Rails - Add HTML attribute without setting a value

試著忘記壹切 提交于 2019-12-05 11:25:16

Try to pass in empty string as follows:

image_tag(my_image, class: 'has-tip', title: "My Title Here", data: { tooltip: "" })

In Rails 4.1.4 using above tip :"data-tooltip" => '' renders data-tooltip without value.

For an attribute with no value like multiple <input multiple type="file">, you can override the attribute default name, by uppercasing and setting an empty string. For example input_html: { Multiple: '' } But this is a hack.

In rails 5, I was trying to add itemscope attribute with no value in the following link tag scenario:

    <%= link_to example_path(@example) do %>
      <span>
        <%= @example.name %>
      </span>
    <% end %>

I needed the resulting html a tag to show the itemscope attribute without any value like so:

<a itemscope href="example/path">
    <span>
       some text
    </span>
</a>

Notice the itemscope has no ="" or itemscope="itemscope".

I tried solutions from SO and other places and the only thing that worked for me was adding the following to the link_to tag: " itemscope" => ''. Notice the space between the first double quote and the word itemscope.

This seems to generate the desired outcome and also validated as schema.org tag on google (that is what i used the tag for).

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