ruby Simple_form:删除带有标签的内联复选框的外部标签

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12114014/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-06 05:22:15  来源:igfitidea点击:

Simple_form: Remove outer label for an inline checkbox with label

rubyruby-on-rails-3simple-form

提问by David Nix

Using Simple_form 2.0.2

使用 Simple_form 2.0.2

The simple form code using HAML:

使用 HAML 的简单表单代码:

= f.input :remember_me, as: :boolean, inline_label: 'Remember me'

But it renders this:

但它呈现这个:

<div class="control-group boolean optional">
  <label class="boolean optional control-label" for="admin_remember_me">Remember me</label>
  <div class="controls">
    <input name="admin[remember_me]" type="hidden" value="0" />
    <label class="checkbox"><input class="boolean optional" id="admin_remember_me" name="admin[remember_me]" type="checkbox" value="1" />Remember me</label>
  </div>
</div>

How do I remove that first label that's rendered, so that I only have the inline label?

如何删除呈现的第一个标签,以便我只有内联标签?

回答by Hernan S.

You can use:

您可以使用:

= f.input :remember_me, as: :boolean, inline_label: 'Remember me', label: false

回答by David Nix

Found a solution after much Google fu.

经过大量的Google fu后找到了解决方案。

Use input_fieldinstead of inputwhich won't automatically generate a label.

使用input_field代替inputwhich 不会自动生成标签。

= f.input_field :remember_me, as: :boolean, inline_label: 'Remember me'

回答by Serge Seletskyy

For whom it doesn't work

对谁不起作用

= f.input_field ...

= f.input_field ...

Use this way

用这种方式

= f.check_box ...

= f.check_box ...

回答by gamov

With simple_form 2.1.0 and rails 3.0.20, none of the solutions listed here worked (I don't want to use f.input_field because it's an admission of defeat).

使用 simple_form 2.1.0 和 rails 3.0.20,这里列出的解决方案都不起作用(我不想使用 f.input_field 因为它承认失败)。

The missing part is the boolean_styleoption:

缺少的部分是boolean_style选项:

options.merge!({label: false, boolean_style: :inline})

I suggest you create a custom input for this (e.g.: inline_checkbox)

我建议您为此创建一个自定义输入(例如:inline_checkbox)

boolean_style is configured as :nested by default, I think:

boolean_style 配置为 :nested 默认情况下,我认为:

# Defaults to :nested for bootstrap config.
#   :inline => input + label
#   :nested => label > input
config.boolean_style = :nested

回答by Marino

Maybe too late, but inspired by gamov answer I have made this a custom wrapper from inline bootstrap checkbox in the initializer file 'config/simple_form_bootstrap.rb':

也许为时已晚,但受 gamov 答案的启发,我已将其从初始化文件“ config/simple_form_bootstrap.rb”中的内联引导程序复选框中的自定义包装器:

config.wrappers :horizontal_radio_and_checkboxes, tag: 'div', class: 'form-group', error_class: 'has-error' do |b|
   b.use :html5
   b.optional :readonly

   b.use :label, class: 'col-sm-3 control-label'
   b.use :input
   b.use :error, wrap_with: { tag: 'span', class: 'help-block' }
   b.use :hint,  wrap_with: { tag: 'p', class: 'help-block' }
 end

which generates this html:

生成这个html:

 <div class="form-group boolean optional user_admin">
    <label class="boolean optional col-sm-3 control-label" for="user_admin">Admin</label>
    <div class="col-sm-9 checkbox-inline">
    <input name="user[admin]" value="0" type="hidden">
    <input class="boolean optional" id="user_admin" name="user[admin]" value="1" type="checkbox">
  </div>

回答by Dreamr OKelly

.control-group.error .help-inline {
  display: none;
}

This should work, it works for me on rails 3.2 and simple_form 2.x+

这应该有效,它适用于 rails 3.2 和 simple_form 2.x+