Question

I want to add a data attribute to a text field tag

<%= text_field_tag(:address) %>

which produces

<input id="address" type="text" name="address") %>

to make it have the HTML of

<input id="address" type="text" name="address" data-clob="ccc") %>

so I tried

<%= text_field_tag(:address, data:{clob: 'ccc'}) %>

but it was added as the actual value instead of an attribute, i.e.

<input id="address" type="text" value="{:data=>{:clob=>"ccc"}}" name="address"></input>

even though for a text field label I had used

<%= label_tag(:address, t("ui.reservations.between_now_and_param", 
param: @start_date.strftime(    time_format)), data:{blob: 'bbb'})%>

as detailed in How to add HTML5 data- attributes to a rails form label tag?

How can I add it as an attribute?

Was it helpful?

Solution

The answer was that a second parameter was required for the value and using "" or ,, didn't work but but using nil did - caused no "value=" attribute to be generated, e.g.

<%= text_field_tag(:address, nil, data:{clob: 'ccc'}) %>

Though I need to see if that works if this form is redisplayed on error...

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top