Rails fields_for form not showing up, nested form

前端 未结 3 1651
轮回少年
轮回少年 2020-12-24 03:45

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

相关标签:
3条回答
  • 2020-12-24 03:57

    Please try

    = f.fields_for :paintings, Painting.new do |p|
    
    0 讨论(0)
  • 2020-12-24 04:04

    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

    0 讨论(0)
  • 2020-12-24 04:05

    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
    
    0 讨论(0)
提交回复
热议问题