Ruby-on-rails 使用默认值检查 rails check_box_tag 设置

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11796629/
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-03 03:47:51  来源:igfitidea点击:

rails check_box_tag set checked with default value

ruby-on-railscheckboxarguments

提问by Jeff Storey

I currently have a rails check_box_tag call that looks like

我目前有一个 rails check_box_tag 调用,看起来像

check_box_tag #{name}

I want to include a checked attribute, which I know I can do with

我想包含一个检查属性,我知道我可以用

check_box_tag name, value, checked

But what if I want to set it to checked without explicitly specifying value(I just want to use the default). Or similarly, what if I wanted to specify html options without specifying the checkedattribute. Is there a way to do this?

但是如果我想在没有明确指定value的情况下将它设置为选中(我只想使用默认值)怎么办。或者类似地,如果我想指定 html 选项而不指定checked属性怎么办。有没有办法做到这一点?

回答by KPheasey

Just wanted to update this. The third parameter for check_box_tagis a boolean value representing the checked status.

只是想更新这个。for的第三个参数check_box_tag是一个布尔值,表示检查状态。

check_box_tag name, value, true

回答by user2516008

If you want the checkbox to be checked, then

如果您希望选中复选框,则

check_box_tag name, value, {:checked => "checked"} 

otherwise

除此以外

check_box_tag name, value

回答by shilovk

check_box_tag(name, value = "1", checked = false, options = {})

check_box_tag(名称,值=“1”,选中=假,选项={})

Examples:

例子:

check_box_tag 'receive_email', 'yes', true
# => <input checked="checked" id="receive_email" name="receive_email" type="checkbox" value="yes" />

check_box_tag 'tos', 'yes', false, class: 'accept_tos'
# => <input class="accept_tos" id="tos" name="tos" type="checkbox" value="yes" />

check_box_tag 'eula', 'accepted', false, disabled: true
# => <input disabled="disabled" id="eula" name="eula" type="checkbox" value="accepted" />

api.rubyonrails.org

api.rubyonrails.org

回答by dimuch

There are no ways to do it directly. But the check_box_tagimplementation is trivial, you can monkey patch it or create own helper.

没有办法直接做到这一点。但是check_box_tag实现是微不足道的,你可以给它打补丁或创建自己的助手。

Original implementation:

原始实现:

  def check_box_tag(name, value = "1", checked = false, options = {})
    html_options = { "type" => "checkbox", "name" => name, "id" => sanitize_to_id(name), "value" => value }.update(options.stringify_keys)
    html_options["checked"] = "checked" if checked
    tag :input, html_options
  end

回答by viks

If anyone have column type boolean then look at this. is_checked? will be default boolean value. It worked for me.

如果有人有列类型 boolean 然后看看这个。is_checked?将是默认的布尔值。它对我有用。

<%= hidden_field_tag :name, 'false' %> <%= check_box_tag :name, true, is_checked? %>

<%= hidden_field_tag :name, 'false' %> <%= check_box_tag :name, true, is_checked? %>

回答by Taylor Roozen

If you pass in a value of "1" to the value field, it will pass-on the value of the real state of the checkbox, regardless of the checked default value:

如果向 value 字段传递值“1”,它将传递复选框的真实状态值,而不管选中的默认值:

is_checked = <default boolean>

check_box_tag :show_defaults, '1', is_checked

(now always reflects user input)

(现在总是反映用户输入)