【问题标题】:Title case for simple_form labelssimple_form 标签的标题大小写
【发布时间】:2016-06-01 07:52:35
【问题描述】:

以下代码给出了“项目地址”的标签:

%h2 New Project
= simple_form_for(@project, html: {class: "form-horizontal"}, wrapper: :horizontal_form) do |f|
  = f.error_notification
  = f.input :project_address, required: true
  = f.button :submit, "Create"

除了使用f.label之外,还有什么方法可以使标签标题大小写(“项目地址”)?

【问题讨论】:

    标签: ruby-on-rails simple-form capitalization


    【解决方案1】:

    如果你想为所有标签命名,你可以在初始化器中配置简单的表单

    # config/initializers/simple_form.rb
    # How the label text should be generated altogether with the required text.
    config.label_text = lambda { |label, required, explicit_label| "#{required} #{explicit_label ? label : label.to_s.titleize}" }
    

    这将使您的所有标签标题大小写,除非您使用 :label 选项明确指定标签。

    【讨论】:

    • 对于那些想知道的人,explicit_label 是一个布尔值,而不是一个字符串。
    • 我觉得这里不需要to_s调用,label.titleize应该足够了
    猜你喜欢
    • 2018-08-19
    • 1970-01-01
    • 2013-06-03
    • 1970-01-01
    • 1970-01-01
    • 2014-07-31
    • 2016-01-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多