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?
See
label’s documentation (and on APIdock).