Rails:simple_form:从复选框集合中获取空字符串 [英] Rails : simple_form : Getting an empty string from checkbox collection

查看:24
本文介绍了Rails:simple_form:从复选框集合中获取空字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的视图中有以下代码

<%= f.input :role_names, as: :check_boxes, collection: @program.role_names %>

每当我提交表单时,我都会得到类似 ["admin, "moderator", ""] 的值,但我期待的是类似 ["admin, "moderator"],这是为什么?

And whenever I submit the form I am getting values something like ["admin, "moderator", ""] but I was expecting something like ["admin, "moderator"] , why is this?

而且我做了一个inspect元素,在最后一次check之后有一个<input name="user[role_names][]" type="hidden" value="">标签框,在同一控制组内.我想这是在 params[:user][:recipient_role_names] 的最后添加的.

Moreover I made a inspect element, and there was a <input name="user[role_names][]" type="hidden" value=""> tag after the last check box, within the same control-group. I suppose this is getting added at the last in the params[:user][:recipient_role_names].

我该如何处理?我知道我可以做一个 reject(&:blank?) 但是在 params[:user][:recipient_role_names] 上有更简洁的方法吗?我还想知道为什么要添加输入元素?是简单形式的错误还是我做错了什么?

How should I handle this? I know I can do a reject(&:blank?) but is there a cleaner way on params[:user][:recipient_role_names]? I also want to know why the input element is getting added? Is it a bug in simple form or I have done something wrong?

其他信息:

  1. simple_form gem 版本:2.0.4
  2. rails 版本:3.2.8

推荐答案

这是 Rails 的特性.您可以在 Rails 4 中禁用它.您可以在 simple form issue #603Rails 问题 #5402

It's a Rails' feature. You'll be able disable it in Rails 4. You can read more about this on simple form issue #603 and Rails issue #5402

这篇关于Rails:simple_form:从复选框集合中获取空字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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