简体   繁体   English

如何告诉我的simple_form对象返回仅包含选定ID而不包含任何空值的数组?

[英]How do I tell my simple_form object to return an array with just the selected IDs and not any empty values?

So I have a simple_form object that looks like this: 所以我有一个simple_form对象,看起来像这样:

<%= f.input :parents, collection: (@node.family_tree.nodes - @node.parents - [@node]).uniq, as: :check_boxes, label: "Parent 1" %>

This produces HTML that looks like this: 这将产生如下所示的HTML:

<div class="node_parents">
<label>Parent 1</label>
<input id="node_parents_13" name="node[parents][]" type="checkbox" value="13" /><label>Jack</label>
<input id="node_parents_35" name="node[parents][]" type="checkbox" value="35" /><label>Testy</label>
<input id="node_parents_37" name="node[parents][]" type="checkbox" value="37" /><label>Resty</label>
<input id="node_parents_36" name="node[parents][]" type="checkbox" value="36" /><label>Mesty</label>
<input name="node[parents][]" type="hidden" value="" />
</div>

When I fill out 2 of the 4 objects, my log looks like this: 当我填写4个对象中的2个时,我的日志如下所示:

Parameters: {"utf8"=>"✓", "authenticity_token"=>"F1HGLwIjDG3VaIYlnsu7NbROEiWBO8xqJtjh5MreI9E=", "node"=>{"name"=>"Lesty", "parents"=>["13", "35", "", ""], "family_tree_id"=>"2"}, "commit"=>"Update Node", "id"=>"38"}

Rather than it sending the params[:parents] as a 4-element array with 2 empty/nil values, I would rather it just send a 2-element array. 与其将params[:parents]作为具有2个空/零值的4元素数组发送,我宁愿它仅发送2元素数组。

How do I do that? 我怎么做?

I don't know if you really need to change what is sent by the form, or if you can just filter out the blank values in the controller before doing anything with them? 我不知道您是否真的需要更改表单发送的内容,或者是否可以在对它们执行任何操作之前过滤掉控制器中的空白值?

If you do want to avoid sending empty values then you will need to do something with javascript on form submit, to alter the params from the form before they are submitted. 如果确实要避免发送空值,则需要在表单提交上使用javascript做一些操作,以在提交表单之前更改表单中的参数。 It's definitely much simpler to let the form submit the blanks and filter them out in the controller, however. 但是,让表单提交空白并在控制器中将其过滤掉绝对要简单得多。

To do it with javascript, you'll need to do something like this: i'm assuming you're using jQuery here. 要使用javascript,您需要执行以下操作:我假设您在此处使用jQuery。 The approach i have chosen is to change the submit button so it calls a function instead of submitting the form. 我选择的方法是更改​​“提交”按钮,以便它调用一个函数而不是提交表单。 The function disables the empty inputs (thus not including them in the form data) and then submits the form. 该功能禁用空输入(因此不将其包括在表单数据中),然后提交表单。

Add a js function to the page 将js函数添加到页面

<script type="text/javascript">
  var processForm = function(){
    //disable unchecked checkboxes
    $("#myForm input[type=checkbox]").not(":checked").attr("disabled", "disabled");
    $("#myForm").submit();
  };
</script>

then replace your submit button with this: 然后将您的提交按钮替换为:

<%= button_to_function "Submit", "processForm();" %>

Try this 尝试这个

params[:node][:parents].reject!{|p| p == ""} 

or 要么

params[:node][:parents].reject!(&:blank?)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM