simple_form custom input with custom wrapper

前端 未结 1 1413
北荒
北荒 2021-02-02 18:36

I\'m trying to make a custom input for currency in my app. I had those bootstrap wrappers and etc (I think it comes with simple_form or with bootstrap gem...), so, I could do so

相关标签:
1条回答
  • 2021-02-02 18:40

    That block variable is not existent, your input method have to be like this:

    class CurrencyInput < SimpleForm::Inputs::Base
    
      def input
        input_html_classes.unshift("string currency")
        input_html_options[:type] ||= input_type if html5?
    
        template.content_tag(:span, "$", class: "add-on") +
          @builder.text_field(attribute_name, input_html_options)
      end
    end
    

    Now you can register a default wrapper to this custom input in you Simple Form initializer:

    config.wrapper_mappings = { :currency => :append }
    

    An you can use like this:

    <%= f.input :cost, :as => :currency %>
    
    0 讨论(0)
提交回复
热议问题