Creating custom attributes in check_box_tag

I would like to create a checkbox with custom html attributes (later use UJS). 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?

+8
ruby-on-rails ruby-on-rails-3
source share
2 answers

You must specify custom attributes as fourth arguments, parameters. The first three arguments are: name, value = "1", checked = false. See check_box_tag .

The code may be like this:

 <%= check_box_tag :name, 1, false, data: { "toggle-completed" => "" } %> 
+21
source share

I used the JS stimulus, so for the user attribute data-action I did the following

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

All Articles