【问题标题】:Nest Input Inside Label with Simple_Form and Rails 4使用 Simple_Form 和 Rails 4 在标签内嵌套输入
【发布时间】:2016-03-19 12:17:41
【问题描述】:

我在一个应用程序中使用 Rails 4 和 Simple_Form,对于我必须实现的前端布局,我需要能够在标签标签中包含输入标签。输出需要如下所示:

<label class="input string optional profile_first_name">
    <input class="string optional" type="text" value="" name="profile[first_name]" id="profile_first_name">
</label>

我尝试创建一个如下所示的自定义包装器:

config.wrappers :custom_form, tag: 'div', class: 'form-group', error_class: 'has-error' do |b|
  b.use :html5
  b.use :placeholder
  b.optional :maxlength
  b.optional :pattern
  b.optional :min_max
  b.optional :readonly

  b.wrapper tag: 'label' do |ba|
   ba.use :input
   ba.use :error, wrap_with: { tag: 'span', class: 'help-block' }
   ba.use :hint,  wrap_with: { tag: 'p', class: 'help-block' }
  end
end

我遇到的问题是生成的 html 缺少 Smart_Form 通常生成的标签文本。我确信必须有一种方法来正确格式化包装器以显示标签标签内的输入并且标签仍然显示,但我不知所措。提前致谢!

【问题讨论】:

    标签: ruby-on-rails simple-form


    【解决方案1】:

    您应该能够简单地将ba.use :label_text 插入到您希望标签出现的任何位置。

    b.wrapper tag: 'label' do |ba|
       ba.use :label_text
       ba.use :input
       ba.use :error, wrap_with: { tag: 'span', class: 'help-block' }
       ba.use :hint,  wrap_with: { tag: 'p', class: 'help-block' }
    end
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-08-16
      • 2020-06-15
      • 1970-01-01
      • 2011-12-02
      • 1970-01-01
      • 1970-01-01
      • 2011-05-18
      相关资源
      最近更新 更多