I want to save the form input contents in the DB, but I was addicted to it, so output
Rails 5.0.7.2 ruby 2.5.1 mysql 14.14
When I try to save the form input, it is saved in the parameter but not in the DB.
Check the cause by adding! After the method to save.
Then, ** validation failed: User must exist ** (error content may be different)
The cause is that the foreign key is not included when the association is formed and it is played by validation.
Describe ** optional: true **.
goal.rb
class Goal < ApplicationRecord
validates :name, presence: true, uniqueness: true
validates :time, presence: true, uniqueness: true
validates :days, presence: true, uniqueness: true
belongs_to :user, optional: true #Edit here
end
** optional: true ** allows nil for the foreign key of belongs_to.
I think you can save it in the DB with this. please refer!
Recommended Posts