I want to save the form input contents in DB, but I was addicted, so output
Rails 126.96.36.199 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.
Add! After the method to save and check the cause.
Then, validation failed:User must exist (error content may differ)
The cause is that the relevant foreign key is not entered and the validation is played when the association is set up.
Describe optional: true.
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 is to allow nil foreign keys of belongs_to.
Now you can save it in DB. please refer!