How to change label class using Formtastic

I have the following code using the latest Formatastic 3.1.0.

<%= f.input :name, :input_html => { :class => "col-lg-10" }, :label_html => { :class => "col-lg-2" }%>

      

I can change the input_html, but I cannot find an option to change the html label class. Is there a way to do this?

The output is generated as such (note the omission of "col-lg-2" in the label class).

<li class="string input required stringish" id="account_name_input">
<label for="account_name" class="label">Name<abbr title="required">*</abbr></label>
<input id="account_name" class="col-lg-10" type="text" value="" name="account[name]">
</li>

      

+3


source to share


1 answer


Looking through the documentation and source code, I don't see a built-in way to do this. (I could always be wrong.) However, perhaps this monkey patch will work:

config / initializers / formtastic_monkey_patch.rb

Formtastic::Inputs::Base::Labelling.module_eval do

  def label_html_options
    {
      :for => input_html_options[:id],
      :class => (['label'] + (options[:label_class] || [])).flatten
    }
  end
end

      



This should override the default Formtastic function label_html_options

that can be seen in this file . Then the classes are added via an array:

f.input :my_attribute, :label_class => ['my_class', 'my_other_class']

      

This is my idea, but I would suggest getting an opinion separate from mine before using it.

+1


source







All Articles