views:

172

answers:

3

I'm constructing a simple form in ERB but the HTML produced by the text_field tag makes the for attribute in the label tag invalid.

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

Produces the 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>

Which results in the error

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

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

A: 

Take it out of the quotes, or generate the div content as a string and add it to the div.innerHTML

StingyJack
A: 

The for attribute is supposed to reference the ID attribute of the element for which it is the label, not its name.

Therefore, don't you need:

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

...?

Lee Kowalkowski
A: 

Thanks for the clarification Lee. I was getting it from a bogus example I guess.

Michael Glenn