Rails checkbox array choose nothing

I m getting the computer_ids of user like that

<%= check_box_tag "user[computer_ids][]", computer.id, @user.computers.include?(computer) %>

If I choose at least one from checkbox array, that's fine. But if I did not choose any thing it give me an array like that

undefined method `[]' for nil:NilClass

That's because there is no parameter comes. I did not find a solution. The relation is habtm

I'm try to control it is empty or not

if ( !(params[:user][:computer_ids].empty?)  )


since computer_ids is the only parameter in users it didn't understand what params[:user] is and that's why it gives an error.

if ( !(params[:user].present?)  ) , has solved the problem


Add hidden field above all check-boxes with empty value. It will be sent in case user didn't check any check-boxes like:-

<%= form_for @user do |f| %>
    <%= hidden_field_tag "user[computer_ids][]", nil %>
    <% Computer.all.each do |computer| %>
        <%= check_box_tag "user[computer_ids][]", computer.id, @user.computers.include?(computer) %>
    <% end %>
    <%= f.submit 'Submit'%>
<% end %>

Reject blank values from params like:-

params[:user][:computer_ids] = params[:user][:computer_ids].reject { |c| c.empty? }

Need Your Help

Excel filter from other list

excel drop-down-menu filter

I have 2 lists in excel. First one is for searching (i want to have dropboxes), and second list is for data.

Possibly useless Linear Layout (I don't think so)

android android-layout android-widget android-linearlayout

I'm doing an Android layout and the compiler says "This LinearLayout layout or its LinearLayout parent is possibly useless". I want a few edit texts and buttons to be in a vertical layout like a fo...