简单表单自定义组件不向输入添加属性

Simple Form custom component does not add attribute to input

我想要一个添加 2 个东西的组件。首先,反馈给用户,看看还剩多少字符(稍后通过 JS 更新)。其次,给JS使用的输入的属性(字段限制)。

我关注了 this, and pretty much just coping/pasting from one 他们的内置组件。反馈显示正确,但 :maxlength 不存在于输入中。

module SimpleForm                                                                                                                                                             
  module Components                                                                                                                                                           
    module CharCounter                                                                                                                                                        
      def char_counter(wrapper_options = nil)                                                                                                                                 
        if options[:char_counter].present?                                                                                                                                    
          input_html_options[:maxlength] = limit                                                                                                                              
          "<span>#{limit - object.read_attribute(attribute_name).length }</span>".html_safe                             
        end                                                                                                                                                                   
      end                                                                                                                                                                     

      def has_char_counter?                                                                                                                                                   
        char_counter.present?                                                                                                                                                 
      end                                                                                                                                                                     
    end                                                                                                                                                                       
  end                                                                                                                                                                         
end

wrapper.rb

...
b.optional :char_counter, wrap_with: { tag: 'p', class: 'help-block text-right' }
...

html输出

<div class="form-group string required activity_name">
  <label class="string required control-label" for="activity_name">
    Name <abbr title="required">*</abbr>
  </label>
  <input class="string required form-control" type="text" value="" name="activity[name]" id="activity_name">
  <p class="help-block text-right">
    <span>73</span>
  </p>
</div>

谢谢!

所以在对 SimpleForm 代码进行了几天的挖掘之后,我意识到 元素顺序很重要

wrapper.rb

b.optional :char_counter, wrap_with: { tag: 'p', class: 'help-block text-right' }

必须在

之前
b.use :input