使用Simple_Form和rails 4在标签中嵌套输入 [英] Nest Input Inside Label with Simple_Form and Rails 4

查看:11
本文介绍了使用Simple_Form和rails 4在标签中嵌套输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我为一个应用程序使用了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通常生成的标签文本。我确信必须有一种方法来正确格式化包装器,以显示标签标记内的输入,并使标签仍然显示,但我感到困惑。提前感谢!

推荐答案

您应该能够简单地将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

这篇关于使用Simple_Form和rails 4在标签中嵌套输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆