Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Rails, how do I generate form labels without symbols that still create correct "for" attributes?

In Rails, how do I generate form labels without symbols that still create correct "for" attributes?

If I take this form:

<% form_for(@thing) do |f| %>
  <%= f.error_messages %>
  <p>
    <%= f.label :name %><br />
    <%= f.text_field :name %>
  </p>
  <p>
    <%= f.submit 'Create' %>
  </p>
<% end %>

And alter it to improve the clarity of what's expected in the field:

<% form_for(@thing) do |f| %>
  <%= f.error_messages %>
  <p>
    <%= f.label "What would you like to call your thing?" %><br />
    <%= f.text_field :name %>
  </p>
  <p>
    <%= f.submit 'Create' %>
  </p>
<% end %>

The for attribute on the label tag would read "thing_What would you like to call your thing?", which obviously destroys its relationship with the intended partner field.

So how do I alter the label text while preserving that relationship?

like image 523
Trevor Bramble Avatar asked Mar 28 '09 15:03

Trevor Bramble


1 Answers

<%= f.label :name, "What would you like to call your thing?" %>

See label’s documentation (and on APIdock).

like image 190
amitkaz Avatar answered Sep 23 '22 04:09

amitkaz