I built an app with enums and all works perfectly. However, on a different app where I have nested forms I'm having a small problem. Even though everything seems to work fine in the backend, on my view does not shows the result and says that I have an undefined method
Do anyone has the same problem or is just me??
My Schema:
create_table "estimate_variants", force: :cascade do |t|
t.integer "unit", default: 0
t.bigint "estimate_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["estimate_id"], name: "index_estimate_variants_on_estimate_id"
end
My main model
class Estimate < ApplicationRecord
has_many :estimate_variants
accepts_nested_attributes_for :estimate_variants
end
My nested model
class EstimateVariant < ApplicationRecord
enum unit: { SF: 0, LF: 1 }
belongs_to :estimate
end
My view
<%= form.fields_for :estimate_variants do |builder| %>
<div class="field col s12">
<%= builder.label :unit, 'Unit' %>
<%= form.select(unit: EstimateVariant.units.keys) %>
</div>
Can someone please guide me a bit into this so I can fix it??
UPDATED
I think the problem is the select
helper in your view, try with:
<%= builder.select(:unit, EstimateVariant.units.keys) %>
I made an example to test and work for me, and the enums in the models also work defining only the keys, and the values increase in an ascending way.
You can define:
enum unit: { sf: 0, lf: 1 }
or
enum unit: [:sf, lf]
enum unit: %i[sf lf]