Generate custom attributes in check_box_tag
Asked Answered
L

2

8

I'd like to generate check box with custom html attributes (to use UJS later). Here is my view code

<%= check_box_tag "data-toggle-completed" => "" %>

it gives me

<input id="__data-toggle-completed______" name="{&quot;data-toggle-completed&quot;=&gt;&quot;&quot;}" type="checkbox" value="1">

But I wanted

  <input type="checkbox" data-toggle-completed="">

How can I achieve this?

Leckie answered 24/11, 2012 at 11:26 Comment(0)
A
27

You must give the custom attributes as fourth arguments, options. The first three arguments are name, value = "1", checked = false. See check_box_tag.

The code could be like this:

<%= check_box_tag :name, 1, false, data: { "toggle-completed" => "" } %>
Aeropause answered 24/11, 2012 at 11:47 Comment(0)
L
1

I was using stimulus js so for custom data-action attribute I did the following

<%= check_box_tag :select_shipping_address, true , true, data:{action:"change->form#show_form"}%>

Louisiana answered 27/8, 2019 at 19:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.