Cocoon,嵌套字段内的嵌套字段

时间:2015-06-03 08:06:49

标签: ruby-on-rails cocoon-gem

我有一个项目,项目模型,主要组模型和交易模型,然后每个项目可以有许多主要组,然后每个主要组可以有许多交易。

我可以创建项目和主要组,但交易没有保存。

project.rb:

class Project < ActiveRecord::Base
    belongs_to :user

    has_many :consultants
    accepts_nested_attributes_for :consultants, :reject_if => :all_blank, :allow_destroy => true

    has_many :contractors
    accepts_nested_attributes_for :contractors, :reject_if => :all_blank, :allow_destroy => true

    has_one :client
    accepts_nested_attributes_for :client

    has_many :main_groups
    accepts_nested_attributes_for :main_groups, :reject_if => :all_blank, :allow_destroy => true

    has_many :trades, through: :main_groupsp
end

trade.rb

class Trade < ActiveRecord::Base
  belongs_to :main_group
end

main_group.rb

class MainGroup < ActiveRecord::Base
  belongs_to :project

  has_many :trades
  accepts_nested_attributes_for :trades, :reject_if => :all_blank, :allow_destroy => true
end

部分主要群体:

.nested-fields.add-tasks
    .row
        .col-md-12
            .form-horizontal
                .panel.panel-default
                    .panel-heading 
                        Main Group
                    .panel-body
                        .form-group
                            = f.label :name, :class => 'col-sm-1 control-label'
                            .col-sm-11
                                = f.input :name,:label => false, input_html:{class: "form-input form-control"}
                        .form-group
                            = f.simple_fields_for :trades do |trade|
                                = render 'projects/forms/trade_fields', f: trade
                        .trade-links.form-group
                            .col-md-12
                                = link_to_add_association 'Add Trade', f, :trades,  :partial => 'projects/forms/trade_fields', class: "btn btn-default add-button pull-right btn-success"
                        .form-group
                            .col-sm-12
                                = link_to_remove_association 'Remove Main Group', f,class: "btn btn-default pull-right btn-danger"

贸易表格部分:

.nested-fields.add-tasks
    .row
        .col-md-12
            .form-horizontal
                .panel.panel-default
                    .panel-heading 
                        Trade
                    .panel-body
                        .form-group
                            = f.label :name, :class => 'col-sm-1 control-label'
                            .col-sm-11
                                = f.input :name,:label => false, input_html:{class: "form-input form-control"}
                        .form-group
                            = f.label :cost, :class => 'col-sm-1 control-label'
                            .col-sm-11
                                = f.input :cost,:label => false, input_html:{class: "form-input form-control"}
                        .form-group
                            = f.label :start_date, :class => 'col-sm-1 control-label'
                            .col-sm-11
                                = f.input :start_date,:label => false, input_html:{class: "form-input form-control"}
                        .form-group
                            = f.label :end_date, :class => 'col-sm-1 control-label'
                            .col-sm-11
                                = f.input :end_date,:label => false, input_html:{class: "form-input form-control"}
                        .form-group
                            .col-sm-12
                                = link_to_remove_association 'Remove Trade', f,class: "btn btn-default pull-right btn-danger"

0 个答案:

没有答案