Rails 3 - how can I customize text-label in label

2019-04-23 14:28发布

问题:

This is probably a bit low question, but is there any elegant way, how to change the label-text in label helper?

= f.label :name

generate

<input id="car_name" name="car[name]" size="30" type="text">

If I would like to have the text in label, say, Your Car instead of Name, how to do that?

One way is to write the label tag directly as HTML, but this is a bit dirty way...

回答1:

Just add a second string argument to f.label, like this:

label_tag 'name', 'Your name'
# => <label for="name">Your Name</label>

See here