迫使"人性化"名称的字段可以在小写轨3
-
27-10-2019 - |
题
据我所知,将所接受的方式设定的"人性化"名称的字段的在轨3是使用选择:
# config/locales/en.yml
en:
activerecord:
attributes:
member:
username: 'username' # rather than 'Username'
然而,我只是想轨3使用大写版本,其默认的人性化的名称。是否有一个容易的,建立在种方式做到这一点?
一个例子,以帮助澄清:当内部的一个 form_for
, <%= f.label :username %>
显示"Username"默认。我想让它显示"username".
解决方案
的 label
帮助使用默认值 human_attribute_name 把一个属性成人的名字。如果你看一下源, human_attribute_name
尝试一些东西落下前回来 attributes.to_s.humanize
.它试图翻译,然后它寻找一个 :default
选择选项的散列。
因此,最简单/最佳方式获得所需的功能是重写 human_attribute_name
用你自己的,提供了一个 :default
选项,然后呼吁。轨道提供合理的方式做这样的事情与 alias_method_chain
, 所以...
我已经听够了,给我答案!
把以下的任何文件 config/initializers
和重启你的应用程序:
module ActiveModel
module Translation
def human_attribute_name_with_foo attribute, options = {}
human_attribute_name_without_foo attribute, options.merge( :default => attribute.humanize.downcase )
end
alias_method_chain :human_attribute_name, :foo
end
end
其他提示
我有同样的问题。我解决了它通过css:
在foo。html。erb:
<%= f.label :username, :class => "my_class" %>
在酒吧。css:
label.my_class {
text-transform: lowercase;
}
我会更喜欢一种不同的解决方案,也。但是,这是唯一一个我已经能够找到,因此为止。
不隶属于 StackOverflow