如何允许用户从 Rails 的多重选择框中选择零个选项?

发布于 09-12 04:28 字数 316 浏览 8 评论 0原文

我有一个带有 options 列的设置模型,并将其设置为使用 serialize :options 进行序列化。在我看来,我有一个多重选择框,使用 select("settings", "options", ['option1','option2','option3'], {}, :multiple => true)< /code> 只要用户至少选择一个选项,它就可以正常工作。但是,如果他们不选择任何选项,则不会提交任何选项,因此选项不会更新。

如何允许用户从 Rails 的多重选择框中选择零个选项?

I have a settings model with a column options, and set it to serialize with serialize :options. In my view, I have a multiple selection box, using select("settings", "options", ['option1','option2','option3'], {}, :multiple => true) which works fine so long as the user selects at least one option. However, if they don't select any options, no options are submitted and so the options aren't updated.

How do I allow the user to select zero options from a multiple selection box in rails?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

寂寞美少年2024-09-19 04:28:10

这与 Rails 无关:如果“select”元素中未选择任何内容,html 表单不会将此类参数发送到服务器。但你应该能够在控制器中修复它。像这样的东西

if params[:settings] == nil
  params[:settings] = [];
end

不确定是否有更优雅的解决方案。

That has nothing to do with rails: html form won't send such parameter to server if nothing is chosen in 'select' element. But you should be able to fix it in controller. Something like this

if params[:settings] == nil
  params[:settings] = [];
end

Not sure if there's more elegant solution.

最佳男配角2024-09-19 04:28:10

在选择框后面添加一个隐藏字段,将空值发布到“settings[options]”,

这与 Rails 用于确保未选中的复选框被发布为 false 的技巧相同。

Add a hidden field after the select box, that posts an empty value to "settings[options]"

It's same trick that rails uses to make sure unchecked checkboxes get posted as false.

岁月静好2024-09-19 04:28:10

如果未发布属性,我不喜欢假设值为空。它破坏了 Rails 期望更新属性的方式,并且如果您将控制器操作也用于 API 和 HTML,则可能会出现问题。我处理此问题的首选方法是在多选之前添加隐藏的输入字段。

<input type="hidden" value="" name="parent_model[my_attribute_ids][]">

如果您使用 JQuery,您可以自动添加这些隐藏的输入字段:

$('select[multiple="multiple"]').each(function(i){
    $(this).before('<input type="hidden" name="'+this.name+'" value="" />')
});

我意识到这个答案不是很及时,但我希望这会对有类似问题的人有所帮助。

I do not like assuming a value is empty if the attribute is not posted. It breaks the way Rails expects to update attributes, and it can present problems if you are using your controller actions also for APIs as well as HTML. My preferred way of handling this is by adding a hidden input field before multiselects.

<input type="hidden" value="" name="parent_model[my_attribute_ids][]">

If you use JQuery you can automate the addition of these hidden input fields:

$('select[multiple="multiple"]').each(function(i){
    $(this).before('<input type="hidden" name="'+this.name+'" value="" />')
});

I realize this answer is not very timely, but I hope this will help someone with a similar question.

妄司2024-09-19 04:28:10

您可以提供“无”选项。

示例来自: http://api.rubyonrails.org/classes/ActionView/Helpers /FormOptionsHelper.html

select("post", "person_id", Person.all.collect {|p| [ p.name, p.id ] }, {:include_blank => 'None'}, {:multiple => true})

会变成

<select name="post[person_id]" multiple="multiple">
  <option value="">None</option>
  <option value="1">David</option>
  <option value="2" selected="selected">Sam</option>
  <option value="3">Tobias</option>
</select>

You could provide a "None" option.

Example from: http://api.rubyonrails.org/classes/ActionView/Helpers/FormOptionsHelper.html

select("post", "person_id", Person.all.collect {|p| [ p.name, p.id ] }, {:include_blank => 'None'}, {:multiple => true})

Would become

<select name="post[person_id]" multiple="multiple">
  <option value="">None</option>
  <option value="1">David</option>
  <option value="2" selected="selected">Sam</option>
  <option value="3">Tobias</option>
</select>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文