Search code examples
ruby-on-railssimple-formcustom-data-attribute

How to add data-attribute to the form tag in simple_form?


I'm using garlic.js to validate my forms. Garlic.js recommends adding a data-attribute on the form tag.

Here's what I need to generate:

<form data-validate="parsley">

I'm having issues to generate this data attribute on the form tag. I tried everything and nothing worked. Anyone has a solution to this?


Solution

  • Try this:

    <%= simple_form_for @entity, :html => {:"data-validate" => 'parsley'} do |f| %>
        <!-- inputs -->
    <% end %>
    

    Update

    From the comment below, to have the form include an HTML5 data attribute with no value try the following:

    <%= simple_form_for @entity, :html => {:"other-data-value" => ''} do |f| %> 
    

    By setting the attribute to an empty string the form helper renders just the attribute.