【问题标题】:simple_form radio button (need to add <span> tag inside of <label>simple_form 单选按钮(需要在 <label> 内添加 <span> 标签
【发布时间】:2013-10-30 10:54:19
【问题描述】:

使用 simple_form_for gem 创建以下 HTML 的最佳方法是什么?

<label>
   <input name="form-field-radio" type="radio" />
   **<span class="lbl"> radio option 2</span>**
</label>

请注意,默认情况下,当我使用以下语句创建单选按钮时,不会创建上述内容。如何添加该标签?

<%= f.input :state, :collection => Project::STATES, :as => :radio_buttons %>

【问题讨论】:

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


    【解决方案1】:

    我也有类似的需求(在 &lt;label&gt; 中嵌入 &lt;span&gt;)。这不是最干净的解决方案,但它确实有效,我认为通过一些调整它可以让您能够将您的输入和跨度嵌入标签中。以下修改结果:

    <label>Name: 
      <span class="hint">this is a hint...</span>
    </label>
    

    我添加了以下作为初始化程序(使用 rails 4 和 simple_form 3)来覆盖 label_text 方法:

    # initializers/simple_form_custom.rb
    module SimpleForm
      module Components
        module Labels
    
          def label_text
            if hint
              hint_text = %[<span class="hint">#{hint}</span>]
            else
              hint_text = ""
            end
            SimpleForm.label_text.call(raw_label_text, required_label_text, hint_text).strip.html_safe
          end
    
        end
      end
    end
    

    然后在initializers/simple_form.rb 我有:

    config.label_text = lambda { |label, required, hint| "#{label}: #{required} #{hint}" }
    

    【讨论】:

      猜你喜欢
      • 2015-08-17
      • 2016-01-13
      • 2013-04-21
      • 1970-01-01
      • 2012-11-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多