Rails : simple_form : Getting an empty string from checkbox collection

你说的曾经没有我的故事 提交于 2019-12-21 06:51:08

问题


I have the following code in my views

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

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?

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].

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?

Other info:

  1. simple_form gem version: 2.0.4
  2. rails version: 3.2.8

回答1:


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



来源:https://stackoverflow.com/questions/14054164/rails-simple-form-getting-an-empty-string-from-checkbox-collection

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!