Invalid characters in label_tag

I am creating a simple form in ERB, but the HTML generated by the text_field tag invalidates the for attribute in the tag tag.

<div>
  <p><%= label_tag "email[name]", "Name" %></p>
  <%= text_field :email, :name, :class => "text_field" %>
</div>

      

Produces HTML

<div>
  <p><label for="email[name]">Name</label></p>
  <input class="text_field" id="email_name" name="email[name]" size="30" type="text" />
</div>

      

This results in the error

character "[" is not allowed in attribute value "for".

How to generate text without the name of the nested parameter name [name] to change the label tag for an attribute? Is there an alternative approach that produces valid HTML?

0


source to share


2 answers


The for attribute must refer to the ID attribute of the element for which it is a label, not its name.

Therefore, you don't need:



<div>
  <p><%= label_tag "email_name", "Name" %></p>
  <%= text_field :email, :name, :class => "text_field" %>
</div>

      

...

0


source


Take this out from quotes or create div content as string and add it to div.innerHTML



0


source







All Articles