加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby-on-rails – SimpleForm 2:包装器内的输入标签

发布时间:2020-12-17 03:30:13 所属栏目:百科 来源:网络整理
导读:通过使用默认的simple_form 2包装器,它生成一个如下所示的标记: div class="..." label.../label input ... //div 我想得到一个标记,其中输入标记本身在一个包装器中,如下所示: div class="..." label.../label div class="..." input ... / /div/div 我是
通过使用默认的simple_form 2包装器,它生成一个如下所示的标记:

<div class="...">
  <label>...</label>
  <input ... />
</div>

我想得到一个标记,其中输入标记本身在一个包装器中,如下所示:

<div class="...">
  <label>...</label>
  <div class="...">
    <input ... />
  </div>
</div>

我是否必须为此行为创建自定义组件?

谢谢.

尼古拉斯.

解决方法

看一下这个:

包装器API尚未有详细记录,但我花了一些时间试图解决它.这是一个简单的例子,可以在simple_form初始化程序中设置.

# Use this setup block to configure all options available in SimpleForm.
SimpleForm.setup do |config|
  # Wrappers are used by the form builder to generate a complete input.
  # You can remove any component from the wrapper,change the order or even
  # add your own to the stack. The options given to the wrappers method
  # are used to wrap the whole input (if any exists).

  config.wrappers :GIVE_YOUR_WRAPPER_A_NAME,:class => 'clearfix',:error_class => nil do |b|
    b.use :placeholder
    b.use :label
    b.use :tag => 'div',:class => 'WHATEVER_YOU_WANT_TO_CALL_IT' do |ba|
      ba.use :input
      ba.use :error,:tag => :span,:class => :'help-inline'
      ba.use :hint,:class => :'help-block'
    end
  end
end

然后,在创建表单时,只需指定要使用的包装器:

<%= simple_form_for @user,wrapper: 'WHATEVER_YOU_CALLED_YOUR_WRAPPER' do |form| %>
<%end%>

希望这可以帮助.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读