参数“值"如何?在 check_box_tag 工作? [英] How does the argument "value" in check_box_tag work?

查看:54
本文介绍了参数“值"如何?在 check_box_tag 工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

check_box_tag如下:

<块引用>

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

<块引用>

创建一个复选框表单输入标签.

什么是 value 参数?

我在自己的复选框表单中为 value 参数提供了许多不同的值,但复选框似乎工作相同(它工作正常,我只想知道什么 value代码>在做什么?).

似乎其他一些 也被它弄糊涂了.

示例

它们在我的应用程序中做同样的事情(它们都运行良好,这是我没想到的):

<%= check_box_tag :approved, hi there", user.approved?%>

<%= check_box_tag :approved, 1", user.approved?%>

解决方案

value 参数指的是 db 中的布尔值.

  • 如果复选框未选中,则值将为0",布尔值将设置为 false
  • 如果您选中复选框,则值将为1",布尔值将设置为 true

这是 Rails 中复选框的工作方式,如果您想传递一个自定义值,例如 value='ilovemydog' 或其他东西,它不会介意,它会始终引用0"或1".

The docs for check_box_tag are as follows:

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

Creates a check box form input tag.

What is the value argument?

I have provided many different values to the value parameter in my own checkbox form, yet the checkbox seems to work the same (it works, I just want to know what value is doing?).

It seems some others are also confounded by it.

Example

These do the same thing in my application (they both work perfectly, which I wouldn't have expected):

<%= check_box_tag :approved, "hi there", user.approved? %>

<%= check_box_tag :approved, "1", user.approved? %>

解决方案

The value argument refers to the boolean value in the db.

  • If the checkbox is unchecked then the value will be '0' and the boolean will be set to false
  • If you check the checkbox then the value will be '1' and the boolean will be set to true

This is the way checkboxes work in Rails and it doesn't mind if you want to pass a custom value like value='ilovemydog' or something else, it will always refer to '0' or '1'.

这篇关于参数“值"如何?在 check_box_tag 工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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