Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/60.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails Mongoid,布尔形式字段_Ruby On Rails_Mongoid - Fatal编程技术网

Ruby on rails Mongoid,布尔形式字段

Ruby on rails Mongoid,布尔形式字段,ruby-on-rails,mongoid,Ruby On Rails,Mongoid,这是一个关于如何构造布尔类选择和表单字段本身的问题。例如,如果用户在注册时必须选择“wizard”或“archer”之类的团队(出于演示目的),那么我应该选择: field :wizard, type: Boolean field :archer, type: Boolean 但是,它们只能是一个或另一个,那么我如何使用无线电字段构造表单,以便用户只能选择一个或另一个,我不确定是否正确,因此在我的表单中,我会说: <p><%= f.label "wizard" %>&l

这是一个关于如何构造布尔类选择和表单字段本身的问题。例如,如果用户在注册时必须选择“wizard”或“archer”之类的团队(出于演示目的),那么我应该选择:

field :wizard, type: Boolean
field :archer, type: Boolean
但是,它们只能是一个或另一个,那么我如何使用无线电字段构造表单,以便用户只能选择一个或另一个,我不确定是否正确,因此在我的表单中,我会说:

<p><%= f.label "wizard" %><%= radio_button(:user, :wizard, "True") %></p>
<p><%= f.label "archer" %><%= radio_button(:user, :archer, "True") %></p>
<p><%= f.label "wizard" %><%= radio_button(:user, :role, "wizard") %></p>
<p><%= f.label "archer" %><%= radio_button(:user, :role, "archer") %></p>


但这不起作用,因为您可以选择任意多的项目。如何防止用户选择多个收音机选项

单选按钮在HTML的不同字段中不太起作用。为了使它们相互关联,以便您只能选择一个,它们的“名称”属性必须具有相同的值。您的名称值为“user.wizard”和“user.archer”。您将需要以下内容:

<p><%= f.label "wizard" %><%= radio_button(:user, :wizard, "True") %></p>
<p><%= f.label "archer" %><%= radio_button(:user, :archer, "True") %></p>
<p><%= f.label "wizard" %><%= radio_button(:user, :role, "wizard") %></p>
<p><%= f.label "archer" %><%= radio_button(:user, :role, "archer") %></p>

然后在模型中编写代码,删除所选角色以适当设置布尔字段

但总体而言,我建议将您的数据模型更改为将role作为字符串字段,或者规范化role\u id并使其成为真实字段,指向包含角色的单独表(user-belient\u-to:role,role-has\u-many:users)。这样,如果您添加了一个新角色,则不需要更改任何代码,只需向表中添加一行即可。您仍然可以在用户模型上使用布尔方法(例如,User.is_向导?),但它们将根据角色进行计算