简体   繁体   中英

How can I add a custom data attribute to each select using Rails date helper?

I'm using simple_form with Rails' date helper. I'm after two select drop downs for month and year. I have the two select drop downs, but how can I apply a custom data-blah attribute for each select?

<%= p.input :cc_expiration, input_html: { name: nil }, discard_day: true, order: [:month, :year], prompt: true, start_year: Time.now.year, end_year: Time.now.year + 15 %>

If you use f.input , you can achieve it like this:

<%= f.input :year,  as: :select, input_html: { data: { blah: 'value1' } } %>
<%= f.input :month, as: :select, input_html: { data: { blah: 'value2' } } %>

input_html basically gives you the possibility to add additional html attributes and the data symbol lets you define special data-tags. Just pass them as a hash.

One additional note: if you just use f.input for a date time field, simple_form is able of generating the 5 select boxes for year, month, day, hour, minute by itself. In this case, passing input_html will pass those attributes to every select box of the date/time selects.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM