Ruby-on-rails 显示复选框列表而不是多选

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

Display a checkbox list instead of multiple select

ruby-on-railsruby-on-rails-2

提问by pierallard

I have a model MyModelwith a serialized attribute a, describing an array of symbols.

我有一个MyModel带有序列化属性的模型a,描述了一个符号数组。

This code works :

此代码有效:

<% form_for @my_model do |f| %>
  <%= f.select :a, MyModel::AS, :multiple => true) %>
<% end %>

The parameters are correct :

参数正确:

{ :my_model => { :a => [:a_value1, :a_value2] } }

I want to transform this multiple select into a set of checkboxes, like this :

我想将这个多重选择转换为一组复选框,如下所示:

<% form_for @my_model do |f| %>
  <% MyModel::AS.each do |a_value|
    <%= f.check_box(:a_value) %>
  <% end %>
<% end %>

It works too, but the parameters are not the same at all :

它也可以工作,但参数根本不一样:

{ :my_model => { :a_value1 => 1, :a_value2 => 1 } }

I think of 2 solutions to return to the first solution...

我想到了两个解决方案来返回第一个解决方案......

  • Transform my check_boxinto check_box_tag, replace multiple select, and add some javascript to 'check' select values when user clic on check_box_tags. Then, the parameter will be the same directly in the controller.
  • Add a litte code into the controller for 'adapting' my params.
  • 将 mycheck_box转换为check_box_tag,替换多个选择,并在用户单击 check_box_tags 时添加一些 javascript 以“检查”选择值。然后,参数将直接在控制器中相同。
  • 在控制器中添加一小段代码以“调整”我的参数。

What solution is the less ugly ? Or is there any other one ?

什么解决方案不那么难看?或者还有其他的吗?

回答by pierallard

I found a solution, using 'multiple' option that I didn't know.

我找到了一个解决方案,使用我不知道的“多个”选项。

<% MyModel::AS.each do |a_value| %>
  <%= f.check_box(:a, { :multiple => true?}, a_value) %>
<% end %>

Result parameters are a little weird, but it should work.

结果参数有点奇怪,但它应该可以工作。

{"my_model" => { "a" => ["0", "a_value1", "0", "a_value2", "0"] }

Edit from @Viren : passing nilat the end of the function like

从@Viren 编辑:nil在函数的末尾传递,如

  <%= f.check_box(:a, { :multiple => true?}, a_value, nil) %>

works perfectly.

完美地工作。

回答by Vadim

There is another solution worth mentioning that makes it very easy to insert records into the database if you have a has_and_belongs_to_manyor has_manythroughrelationship by using the collection_check_boxesform helper. See documentation here.

还有一个值得一提的解决方案,如果您使用表单助手有一个has_and_belongs_to_manyorhas_manythrough关系,它可以很容易地将记录插入到数据库中collection_check_boxes请参阅此处的文档。

<%= f.collection_check_boxes :mymodel_ids, MyModel::AS, :id, :name do |m| %>
  <%= m.check_box %> <%= m.label %>
<% end %>

Then, in the controller we would allow the mymodel_idsattribute:

然后,在控制器中,我们将允许mymodel_ids属性:

params.require(:mymodel).permit(:name, mymodel_ids:[])

We can also set up a model validation to require that at least one of the checkboxes be checked:

我们还可以设置模型验证以要求至少选中一个复选框:

validates :mymodel_ids, presence: true

An added benefit of this method is that if you later edit the mymodelrecord and uncheck one of the checkboxes, its record will be deleted from the many_to_manyassociation table on save.

这种方法的另一个好处是,如果您稍后编辑mymodel记录并取消选中其中一个复选框,则其记录将从many_to_many关联表中删除save

回答by Moustafa Samir

You can do it like this:

你可以这样做:

<% MyModel::AS.each do |a_value| %>
  <%= f.check_box("a[]", a_value) %>  
<% end %>

This will make params come to server as follows

这将使 params 进入服务器,如下所示

{ :my_model => { :a => [:a_value1, :a_value2] } }

回答by Sujeev

if you already have a select_tag

如果你已经有一个 select_tag

<%= select_tag "filters", options_from_collection_for_select(filter_values, "id", "name", selected_ids), multiple:true, class:"form-control" %>

and wants to replace it with check_box_tag, you will need to implement something like this:

并想用 check_box_tag 替换它,您需要实现如下内容:

<div class="checkbox-inline">
    <%= check_box_tag "filters[]", value.id, selected_ids.include?(value.id), { :multiple => true} %>
    <%= value.name %>
</div>

notice the ending brackets on the name which is needed to catch check box results in the same parameter.

注意捕获复选框所需的名称上的结束括号导致相同的参数。

When I implemented this, the parameters were of the same format between the select_tag and the check_box_tag

当我实现这个时,select_tag 和 check_box_tag 之间的参数格式相同