evfwcqcg
evfwcqcg

Reputation: 16365

Generate custom attributes in check_box_tag

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?

Upvotes: 8

Views: 8874

Answers (2)

gsumk
gsumk

Reputation: 901

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"}%>

Upvotes: 1

Yanhao
Yanhao

Reputation: 5294

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" => "" } %>

Upvotes: 27

Related Questions