看起來像我的表單執行的操作 - 發送正確的值,但不保存在數據庫中。 Check_box_tag需要從enum
(我用枚舉,因爲我使用的選擇字段相同的數據):Rails check_box_tag - 將數組保存到Postgres中
class UserProfile < ApplicationRecord
enum locations: { Kursenai: 0, Papiskes: 1, Versiai: 2 }
而在的form_for:
<% UserProfile.locations.each do |key, val| %>
<%= f.label key %>
<%= check_box_tag('user_profile[locations][]', val, false, id: val) %>
<% end %>
但不能更新:
「 [「0」,「1」]'不是有效位置
Postgres:
t.integer "locations", array: true
因此,我認爲,因爲行類型是integer
失敗,但這樣的:
<%= check_box_tag('user_profile[locations][].to_i', val.to_i, false, id: val) %>
刪除錯誤,但用戶領域:locations
仍然nil
。我錯過了什麼?
強PARAMS:
..permit(locations: [])
附:如果您認爲這可以以更好的方式完成 - 請隨時展示。
然後呢?我仍然會在那裏散列enum –
@ J.D。解釋我的觀點更好一點 – kolas
謝謝,@ kolas,但我看不出有這樣的嚴重理由。枚舉是更輕,在這種方法中,我會創建許多靜態對象,而在課堂上包括枚舉看起來更輕的工作。我現在正在處理簡單的字符串數組,並對其進行哈希處理,因爲之後我可能會刪除或向其添加項目。但我想我一定會用這種方式,如果我的字符串數組將成爲對象數組。感謝您的信息。如果我錯了,請糾正我 –