Search code examples
ruby-on-railssimple-formcapitalization

Title case for simple_form labels


The following code gives a label of "Project address":

%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"

Is there any way of making the label title case ("Project Address") apart from using f.label?


Solution

  • If you wanted to title case all labels, you can configure simple form in the initializer:

    # 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}" }
    

    This will make all your labels title case unless you specify the label explicitly with the :label option.