ERB Radio Button causing string error

I'm working with forms using Embedded Ruby and Rails 4 to create and edit Users. Each User has to be assigned a Role on creation. Initially the form was using check boxes for this and was working fine. Upon changing over to radio buttons however, I get an error.

This is the form:

<%= simple_form_for(@user, html: {class: 'form-horizontal'}) do |f| %>
<%= f.error_notification %>

<div class="form-inputs">
  <%= f.input :name, autofocus: true %>
  <%= f.input :email %>
  <%= f.input :phone_number %>

  <%= f.input :institution_pid, collection: institutions_for_select, as: :select, label: "Institution" %>

  <%= f.association :roles, collection: roles_for_select, as: :radio_buttons %>

  <%= f.input :password %>
  <%= f.input :password_confirmation %>

<div class="form-actions">
  <%= button_tag(type: 'submit', class: "btn doc-action-btn btn-success") do %>
      <i class="glyphicon glyphicon-check"></i> Submit
  <% end %>
  <%= link_to @user, {class: "btn doc-action-btn btn-cancel"} do %>
      <i class="glyphicon glyphicon-remove"></i> Cancel
  <% end %>

I'm specifically asking about the f.association bit. Before, when I was using

as: :check_boxes

it worked exactly as it was supposed to. Now I get this error message:

NoMethodError in UsersController#update

undefined method `reject' for "77":String

I should note that "77" is the value of one of the radio button options.

The method throwing the error is this:

def build_role_ids
  [].tap do |role_ids|
    roles = Role.find(params[:user][:role_ids].reject &:blank?)
    roles.each do |role|
      authorize!(:add_user, role)
      role_ids <<

The HTML when using radio buttons looks like this:

<label class="radio">
  <input class="radio_buttons optional" id="user_role_ids_77" name="user[role_ids]" type="radio" value="77">
  "Institutional Admin"

When using check boxes:

<label class="checkbox">
  <input class="check_boxes optional" id="user_role_ids_77" name="user[role_ids][]" type="checkbox" value="77">
  "Institutional Admin"

If I'm missing something or you need more information, please let me know. Thank you!


With checkboxes, user can select multiple values so you got an Array of role_ids returned in params[:user][:role_ids]. reject method is implemented for Arrays. Hence, it worked in that case.

With radio buttons, only one value would be selected at a time, so you'll get a String value for role_ids in params[:user][:role_ids]. reject method is not implemented for Strings. Hence, the error.

Instead of

params[:user][:role_ids].reject &:blank?

you can check if role_ids is empty or not, as it is a String object.


and update build_role_ids method keeping in mind that role_ids is a String object and not an Array.

This error seems to indicate that params[:user][:role_ids] is not an Array but a single String value. This would make sense since you are changing from checkboxes (multiple values can be selected at a time = Array) to radio buttons (only a single value can be selected at a time = String).

If you truly want to change from checkboxes to radio buttons, then you'll need to update your build_role_ids method logic to expect a single value instead of an array of values.

Need Your Help

What index is buildout really using?

python buildout pypi

I'm trying to install couchdbkit using following buildout config:

NHibernate Select data from table based on non-primary key column

c# nhibernate select data primary-key

I'm brand new to NHibernate and have been fiddling with selecting from a single table based on a criteria. I read that using IQuery is unnecessary if I only want to select data based on an ID, and ...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.