Looks like my form does what is expected to do - sends the right value, but its not being saved in db. Check_box_tag takes data from enum
(I use enum because I use same data for select field):
class UserProfile < ApplicationRecord
enum locations: { Kursenai: 0, Papiskes: 1, Versiai: 2 }
And in form_for:
<% UserProfile.locations.each do |key, val| %>
<%= f.label key %>
<%= check_box_tag('user_profile[locations][]', val, false, id: val) %>
<% end %>
But it fails to update:
'["0", "1"]' is not a valid locations
Postgres:
t.integer "locations", array: true
So I thought it fails because row type is integer
, but this:
<%= check_box_tag('user_profile[locations][].to_i', val.to_i, false, id: val) %>
removed error but user field :locations
is still nil
. What do I miss?
Strong params:
..permit(locations: [])
p.s. if you think this could be done in a better way - please feel free to show.