views:

550

answers:

1

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?

+5  A: 

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

amikazmi
Just came back to say I'd found it in the API docs. Your answer is correct, thanks!
Trevor Bramble