2011-11-17 89 views
5

FormBuilder#label方法(see docs)返回給定屬性的<label>標籤。如果用戶不提供標籤文本,則使用i18n庫進行查找。在檢查的順序是:獲取沒有標籤標籤包裝的國際化標籤文本

  1. helpers.label.object.method
  2. object.class.human_attribute_name(method_name)
  3. method_name.humanize

在哪裏有一個API方法,執行一個類似的查詢,但不包裹在<label>標籤輸出?如果沒有,剝離FormBuilder#label返回的標籤中的<label>標籤的最佳方式是什麼,而不剝離國際化字符串可能包含的任何其他html標籤。

回答

0

目前,我有下面的代碼,它對標籤標籤進行蠻力剝離。尋找更好的解決方案...

label(attribute,text).gsub(/^<label(.*?)>/,'').gsub(/<\/label>$/,'')

+0

看到沒有,我會接受這一點。 –

0

是的,你可以使用Class.human_attribute_name(attribute),例如:

Person.human_attribute_name("first_name") # => "First name" 
# you can also use :first_name 

Source

+2

如果您想使用屬性名稱作爲標籤,這只是準確的。 –