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

Renato Carvalho picture Renato Carvalho · May 13, 2013 · Viewed 16.1k times · Source

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?

Answer

Dan Fairaizl picture Dan Fairaizl · May 13, 2013

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.