Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 834
evfwcqcg Avatar asked Nov 24 '12 11:11

evfwcqcg


2 Answers

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" => "" } %>
like image 69
Yanhao Avatar answered Oct 13 '22 00:10

Yanhao


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

like image 28
gsumk Avatar answered Oct 13 '22 00:10

gsumk