Rails 4,嵌套属性&茧宝石

时间:2016-02-05 02:58:17

标签: ruby-on-rails nested associations cocoon-gem

我正在尝试使用Rails 4制作应用程序。

我有嵌套属性关联,并使用cocoon gem来帮助处理这些表单属性。

我有称为项目,包,参与者和地址的模型。

协会是:

项目

has_one :package
accepts_nested_attributes_for :package

封装

has_one :participant
accepts_nested_attributes_for: participant

参与者

belongs_to :package
has_many :addresses, as: :addressable
accepts_nested_attributes_for :addresses,  reject_if: :all_blank, allow_destroy: true

地址

belongs_to :addressable, :polymorphic => true

Address是多态的。

我的地址字段格式如下:

<div class="nested-fields">

 <div class="container-fluid">

    <div class="form-inputs">
      <div class="row">
        <div class="col-xs-3">
          <%= f.input :unit %>
        </div>
        <div class="col-xs-3 col-xs-offset-1">
          <%= f.input :street_number %>
        </div>
        <div class="col-xs-3 col-xs-offset-1">
          <%= f.input :street %>
        </div>
      </div>


    </div>
    <div class="col-xs-3 col-xs-offset-1">
      <%= f.input :time_zone %>
    </div>


  </div>

  <div class="row">
    <div class="col-xs-3">
      <%= f.input :main_address %>
    </div>
    <div class="col-xs-3 col-xs-offset-1">
      <%= f.input :project_offsite %>
    </div>
  </div>

  <div class="row">
    <div class="col-md-6">
    </div>

  </div>
</div>

<div class = "debug" > <%= yield %> <%= debug(params) if Rails.env.development? %></div>

在我的参与者控制器中,我有:

def new
  @participant = Participant.new
  @participant.addresses_build
end

# GET /participants/1/edit
def edit
  @participant.addresses_build unless @participant.addresses
end

我还将参与者强参数中的所有地址属性都包含为嵌套属性。

在我的包控制器中,我有:

def new
  @package = Package.new
  @package.participants.build
  @package.participant.addresses_build unless @participant.addresses
end

我还在包属性中包含了强参数(作为participant_attributes中的嵌套属性)。

我有参与者的部分字段,其中包含:

<%= f.simple_fields_for :participant do |pf| %>
  <%= pf.error_notification %>

  <div class="form-inputs">

    <%= pf.input :description, :as => :text, :label => "What does participation involve", :input_html => {:rows => 10} %>

    <div class="row">

      <%= pf.input :eligibility, :as => :boolean, :label => false, inline_label: "Are there any eligibility criteria for participants?"  %>

      <%= pf.input :eligibility_criteria, :as => :text, :label => false, inline_label: "Are there any eligibility criteria for participants?",  :input_html => {:rows => 10} %>

      <%= pf.input :location_specific, :as => :boolean, :label => false, inline_label: "Is participation conducted at a specific location (as opposed to remote participation)?"  %>

      <div class="row">

        <div class="intpol2">

          Address for participation
        </div>

        <%= pf.simple_fields_for :addresses do |f| %>

          <%= render 'addresses/address_fields', f: f %>
        <% end %>
      </div>

      <div class="row">
        <div class="col-md-6">

          <%= link_to_add_association 'Add an address', f, :addresses, partial: 'addresses/address_fields' %>

        </div>

      </div>

    </div>
  <% end %>

当我保存所有这些并尝试时,我收到一条错误消息:

上不存在关联地址

我不明白这个错误,因为关联是在地址和参与者之间。参与者属于包。

谁能看到我做错了什么?

0 个答案:

没有答案