26

The first line of code does what I want, the second only capitalizes the first word:

<%= the_label = "Time_Balance".titlecase %><br />
<%= f.label "Time_Balance".titlecase %><br />

I want to titlecase the input label, but I just can't manage it.

This also doesn't work:

<%= the_label = "Time_Balance".titlecase %><br />
<%= f.label the_label %><br />

Nor does this:

<%= the_label = "Time_Balance" %><br />
<%= f.label the_label.titlecase %><br />

3 Answers 3

68

try this.

<%= f.label :time_balance, "Time Balance" %> <br />

Label expects the first argument to be the method_name on the object the form is for, and defaults to just using it, unless you specify it explicitly as part of the second argument which is content/options.

4
  • Thanks for the explanation as well as the solution!
    – Arel
    Apr 18, 2013 at 19:38
  • Mine looks like this: .col-md-2.col-form-label= label_tag(name='First Second') What do I do on this case? Jan 5, 2018 at 22:50
  • Apparently f.label also down cases everything. I was able to preserve case by passing the optional argument like so: <%= f.label :my_text, @question.my_text %>. Mar 3, 2018 at 15:21
  • So weird. I had a label with 3 sentences, and couldn't figure out when the second 2 sentences were being downcased. Thanks!! Apr 17, 2020 at 12:31
2

I can't comment because of my rep. To answer webaholik's question, you can use label_tag in this case:

<%= label_tag :time_balance, "Time Balance" %>
0

F! What if I can't use "f." ?

..such as a secondary filter that doesn't reference a valid attribute

<%= label :time_balance, "Time Balance" %> <br />
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.