I have created an simple rails project. All worked fine until I tried to add a new model Paintings
that belongs_to
treatment and an Patient
Please try
= f.fields_for :paintings, Painting.new do |p|
Try doing following in new action in controller
@patient.treatments.build
Check out build_association part http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html#method-i-belongs_to
You should also read about nested attributes. Use those for reference http://api.rubyonrails.org/classes/ActiveRecord/NestedAttributes/ClassMethods.html
Even the question is quite old, but you are missing the new
that is crucial to this question. The methods destroy
and create
doesn't have anything with this issue. If you have a new
method, which looks something like this:
class TreatmentsController < ApplicationController
def new
@patient = Patient.new
end
end
Then the solution would be do modify the new
method to "build" the paintings
like this:
class TreatmentsController < ApplicationController
def new
@patient = Patient.new
@patient.paintings.build
end
end