NoMethodError: undefined method `learned_on` on `new` learning page
Context: The new.html.erb
view tried to access @learning.learned_on
to set the default value for the date field (value: @learning.learned_on || Date.today
), causing NoMethodError
.
Resolution: I changed the date_field
value in the view to just value: Date.today
. The @learning
instance created by LearningsController#new
(Learning.new
) is unsaved and doesn't have attributes populated from database defaults until saved.
Learning: I learned to be mindful of when database defaults are applied (on save) versus needing to set defaults explicitly in the controller (Model.new(attr: default)
) or view (value: default
) for new object instances presented in forms.
Learned on: April 13, 2024
Edit