如何在simple_form中向表单标记添加数据属性?

时间:2021-03-01 15:57:23

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

我用大蒜。来验证我的表单。大蒜。js建议在表单标记上添加一个数据属性。

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?

我有一些问题要在表单标签上生成这个数据属性。我什么都试过了,但什么都没用。有人能解决这个问题吗?

2 个解决方案

#1


23  

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:

从下面的评论中,有一个没有价值的HTML5数据属性,尝试如下:

<%= 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.

通过将属性设置为空字符串,表单助手只呈现属性。

#2


0  

For anyone stumbling across this in 2016 using either the newer version of Rails (4) or the newer version of Parsley (2) here is what you'll need to do:

对于任何在2016年使用最新版本的Rails(4)或更新版本的Parsley(2)遇到这种情况的人,以下是您需要做的:

First of all to activate parsley with data attributes in the new version you add data-parsley-validate now instead of the data-validate="parsley" from version 1 of Parsley. Now lets get that to work with rails form_for.

现在让我们使用rails form_for。

Even if you're not using parsley, this is how to insert a data attribute in Rails 4.

即使您没有使用parsley,这也是在Rails 4中插入数据属性的方法。

<%= form_tag '/login', :"data-parsley-validate" => '' do %>
    <!-- inputs -->
<% end %>

So basically the difference is not using the html=>{} hash. I tried that to see if that trick would work and at least for me my markup looked like this:

因此,基本上不同之处不在于使用html=>{}哈希。我试着看看这个技巧是否有用,至少对我来说,我的工资是这样的:

<form action="/login" method="post" html="{:data-parsley-validate}">
    <!-- inputs -->
</form>

But by simply putting the data attribute directly in there as a symbol with an empty string as the corresponding value and it worked perfectly. This is the markup I received.

但是,只需将数据属性直接放在那里,作为一个符号,并使用一个空字符串作为相应的值,它就可以很好地工作。这是我收到的加价。

<form action="/login" method="post" data-parsley-validate>
    <!-- inputs -->
</form>

In other words its perfect. So thats how to use Rails 4 to get data attributes in your form_tag.

换句话说,它是完美的。这就是如何使用Rails 4在form_tag中获取数据属性的方法。

#1


23  

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:

从下面的评论中,有一个没有价值的HTML5数据属性,尝试如下:

<%= 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.

通过将属性设置为空字符串,表单助手只呈现属性。

#2


0  

For anyone stumbling across this in 2016 using either the newer version of Rails (4) or the newer version of Parsley (2) here is what you'll need to do:

对于任何在2016年使用最新版本的Rails(4)或更新版本的Parsley(2)遇到这种情况的人,以下是您需要做的:

First of all to activate parsley with data attributes in the new version you add data-parsley-validate now instead of the data-validate="parsley" from version 1 of Parsley. Now lets get that to work with rails form_for.

现在让我们使用rails form_for。

Even if you're not using parsley, this is how to insert a data attribute in Rails 4.

即使您没有使用parsley,这也是在Rails 4中插入数据属性的方法。

<%= form_tag '/login', :"data-parsley-validate" => '' do %>
    <!-- inputs -->
<% end %>

So basically the difference is not using the html=>{} hash. I tried that to see if that trick would work and at least for me my markup looked like this:

因此,基本上不同之处不在于使用html=>{}哈希。我试着看看这个技巧是否有用,至少对我来说,我的工资是这样的:

<form action="/login" method="post" html="{:data-parsley-validate}">
    <!-- inputs -->
</form>

But by simply putting the data attribute directly in there as a symbol with an empty string as the corresponding value and it worked perfectly. This is the markup I received.

但是,只需将数据属性直接放在那里,作为一个符号,并使用一个空字符串作为相应的值,它就可以很好地工作。这是我收到的加价。

<form action="/login" method="post" data-parsley-validate>
    <!-- inputs -->
</form>

In other words its perfect. So thats how to use Rails 4 to get data attributes in your form_tag.

换句话说,它是完美的。这就是如何使用Rails 4在form_tag中获取数据属性的方法。