为什么我的 Rails 多选中的第一个元素总是空白,使用嵌入式数组? [英] Why is the first element always blank in my Rails multi-select, using an embedded array?

查看:21
本文介绍了为什么我的 Rails 多选中的第一个元素总是空白,使用嵌入式数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是Rails 3.2.0.rc2.我有一个 Model,其中我有一个静态 Array,我通过一个表单提供它,以便用户可以选择 Array 并将他们的选择保存到数据库中,存储在 Model 的单个列中.我在存储 Array 的数据库列上使用了序列化,Rails 正确地将用户的选择转换为 Yaml(并在读取该列时返回到数组).我正在使用多选表单输入进行选择.

I'm using Rails 3.2.0.rc2. I've got a Model, in which I have a static Array which I'm offering up through a form such that users may select a subset of Array and save their selection to the database, stored in a single column in Model. I've used serialize on the database column which stores the Array and Rails is correctly converting the users' selections into Yaml (and back to an array when reading that column). I'm using a multi-select form input to make selections.

我的问题是,就我目前拥有的方式而言,除了用户的子集数组在发送到服务器时始终有一个空白的第一个元素之外,一切都按我的预期工作.

这没什么大不了的,事后我可以编写代码来消除它,但我觉得我只是犯了某种语法错误,因为在我看来,默认的 Rails行为会在没有任何原因的情况下有意添加此空白元素.我一定是错过了什么或忘记禁用某种设置.请帮助我了解我遗漏了什么(或者向我指出一些很好的文档,这些文档比我在 intertubes 上找到的内容更深入地描述了这一点).

This isn't a big deal, and I could write code to cut that out after the fact, but I feel like I'm just making some kind of syntactical error as it doesn't seem to me that the default Rails behaviour would intentionally add this blank element without some reason. I must have missed something or forgot to disable some kind of setting. Please help me understand what I'm missing (or point me in to some good documentation that describes this with more depth than what I've been able to find on the intertubes).

  • 包括一个名为 subset_array 的列,它是一个 TEXT 字段
  • includes a column named subset_array which is a TEXT field
  • 序列化:subset_array
  • ALL_POSSIBLE_VALUES = [value1, value2, value3, ...]
  • f.select :subset_array, Model::ALL_POSSIBLE_VALUES, {}, :multiple =>真, :selected =>@model.subset_array
  • 假设只选择了 value1 和 value3
  • "model" =>{subset_array"=>["", value1, value3] }
  • UPDATE 'models' SET 'subset_array' = '--- - "" - value1 - value3 '
  • UPDATE 'models' SET 'subset_array' = '--- - "" - value1 - value3 '

如您所见,数组中有这个额外的空白元素被发送并设置在数据库中.我该如何摆脱它?我的 f.select 调用中是否缺少参数?

As you can see, there's this extra, blank, element in the array being sent and set in the database. How do I get rid of that? Is there a parameter I'm missing from my f.select call?

非常感谢:)

EDIT:这是从 f.select 语句生成的 HTML 代码.看起来好像正在生成一个隐藏的输入,这可能是我的问题的原因?为什么会在那里?

EDIT: This is the generated HTML code from the f.select statement. It looks as though there is a hidden input being generated which may be the cause of my issue? Why is that there?

<input name="model[subset_array][]" type="hidden" value>
<select id="model_subset_array" multiple="multiple" name="model[subset_array][]" selected="selected">
    <option value="value1" selected="selected">Value1</option>
    <option value="value2">Value2</option>
    <option value="value3" selected="selected">Value3</option>
    <option...>...</option>
</select>

推荐答案

隐藏字段是导致问题的原因.但它存在是有充分理由的:当所有值都被取消选择时,您仍然会收到一个 subset_array 参数.来自 Rails 文档(您可能需要向右滚动才能看到所有这些):

The hidden field is what is causing the issue. But it is there for a good reason: when all values are deselected, you still receive a subset_array parameter. From the Rails docs (you may have to scroll to the right to see all of this):

  # The HTML specification says when +multiple+ parameter passed to select and all options got deselected
  # web browsers do not send any value to server. Unfortunately this introduces a gotcha:
  # if an +User+ model has many +roles+ and have +role_ids+ accessor, and in the form that edits roles of the user
  # the user deselects all roles from +role_ids+ multiple select box, no +role_ids+ parameter is sent. So,
  # any mass-assignment idiom like
  #
  #   @user.update_attributes(params[:user])
  #
  # wouldn't update roles.
  #
  # To prevent this the helper generates an auxiliary hidden field before
  # every multiple select. The hidden field has the same name as multiple select and blank value.
  #
  # This way, the client either sends only the hidden field (representing
  # the deselected multiple select box), or both fields. Since the HTML specification
  # says key/value pairs have to be sent in the same order they appear in the
  # form, and parameters extraction gets the last occurrence of any repeated
  # key in the query string, that works for ordinary forms.

最后一段建议您在选择某些内容的情况下不应该看到空的,但我认为这是错误的.向 Rails 做出此承诺的人(参见 https://github.com/rails/rails/commit/faba406fa15251cdc9588364d23c687a14ed6885) 正在尝试使用 Rails 用于复选框的相同技巧(如此处所述:https://github.com/rails/rails/pull/1552),但我认为它不适用于多选框,因为在这种情况下发送的参数形成一个数组,所以没有值被忽略.

The last paragraph suggests that you shouldn't be seeing the empty one in the case when something is selected, but I think it is wrong. The person who made this commit to Rails (see https://github.com/rails/rails/commit/faba406fa15251cdc9588364d23c687a14ed6885) is trying to do the same trick that Rails uses for checkboxes (as mentioned here: https://github.com/rails/rails/pull/1552), but I don't think it can work for a multiple select box because the parameters sent over form an array in this case and so no value is ignored.

所以我的感觉是这是一个错误.

So my feeling is that this is a bug.

这篇关于为什么我的 Rails 多选中的第一个元素总是空白,使用嵌入式数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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