jeudi 30 juin 2016

Rails 4: HATMB Checkbox issue -- nil error

I have a HABTM relationship between learning objectives and tasks. I was able to implement checkboxes via http://ift.tt/29g2W9Z. However, I am running into an issue when all the boxes are unchecked. See blow

form:

<% for task in Task.all %>
<%= check_box_tag "tlo[task_ids][], task.id, @tlo.tasks.include?(task) %>
<%= task.name%>
<%end%>

TLO controller:

    def update
     params[:tlo][task_ids] ||= []
     respond_to do |format|
       if(@tlo.update)
        format.html {redirect_to @tlo}
       end
      end
   end

Yes, task_ids: [] in my permitted params. When looking in the log its says "undefined method [] for nil:nilClass." Some resources say because on a form with only checkboxes with none selected won't submit anything? Does anyone have suggestions for a workaround?




Aucun commentaire:

Enregistrer un commentaire