Nested Attributes in Rails 3

前端 未结 3 719
忘掉有多难
忘掉有多难 2020-12-11 07:09

can anyone please walk me through Nested Attributes in Rails 3?

I have two Models: Certificates and Custodians, related as follows:

Certificate Model:

<
相关标签:
3条回答
  • 2020-12-11 07:27

    accepts_nested_attributes_for should go on the side of one in the one-to-many relationship.

    class Custodian < ActiveRecord::Base
      has_many :certificates
      accepts_nested_attributes_for :certificates
    end
    

    So, in your view, there should be no fields_for :custodian, it's on the wrong side. If you have to build a certificate from that view, you have to list custodians available, probably in a select box.

    0 讨论(0)
  • 2020-12-11 07:29

    This line

    <% f.fields_for :custodian do |custodian| -%>
    

    should be

    <%= f.fields_for :custodian do |custodian| -%>
    
    0 讨论(0)
  • 2020-12-11 07:40

    With a belongs_to, it should be

    @certificate.build_custodian
    
    0 讨论(0)
提交回复
热议问题