简体   繁体   中英

How to add html attributes without a value to a form in Rails?

If I have this form:

<%= form_for @person do |f| %>
  First name: <%=  f.text_field :first_name %>
  ...
<% end %>

how do I add an html attribute to both form_for and text_field without assigning a value?

A) form_for - I want to add the attribute data-parsley-validate here, but the best one I came up with is:

<%= form_for @person, html: {'data-parsley-validate' => ''}  %>

However this produces data-parsley-validate='' . How do I get rid of the ='' ?

B) text_field . I want to make the first name field required. I could write:

<%= f.text_field :first_name, required: ''  %>

but this produces required="required" and not just required . While in the case of required this is ok, there might be other cases (such as data-parsley-no-focus ), where I just need the attribute with no value.

How do I do that?

For what it's worth, HTML5 spec says that these attributes are equivalent:

<input disabled>
<input disabled="">

http://www.w3.org/TR/html-markup/syntax.html#syntax-attr-empty

Note that empty attribute syntax is exactly equivalent to specifying the empty string as the value for the attribute, as in the following example.

<input disabled="">

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM