When a user submits without any boxes checked. I need my ODM (Mongoid) to update the record appropriately.
Having a bit of trouble with:
<% Notification.all.each do |notification| %>
<li>
<%= check_box_tag 'user[notification_ids][]', notification.id, @user.notifications.include?(notification) %>
<%= label_tag notification.description %>
</li>
<% end %>
The doc suggests that the check_box helper puts in a hidden input. The hidden field has the same name and its attributes mimic an unchecked check box. However, with the above code. I am going through a loop. Which is slightly different.
I tried:
<%= check_box('user_notification_ids_', '', options = {:index => notification.id, :checked => @user.notifications.include?(notification)}, checked_value = "1", unchecked_value = "0") %>
But whenever I submit, I get: illegal ObjectId format
Or should I create the hidden tag for notification_ids manually? Something like:
<%= hidden_field_tag 'user[notification_ids][]', '[]' %>
Looking to hear your feedback
Go with the first loop you included. It looks much simpler and looks like it does the same thing.
If I understand your first sentence correctly, it sounds like you aren’t seeing the user’s notifications getting updated to none when they uncheck all the check boxes. When there are no check boxes checked, the browser won’t send that param back. It simply won’t be included in
paramsso when you do something like:… notifications won’t get updated. In your controller, do this to ensure there is something set for
params[:user][:notifications]:This is will set it to an empty array if there if it doesn’t exists and/or there’s no value there. This ensures that
update_attributeswill set it to none/empty.