1

我想用繭寶石來構建嵌套窗體。Rails 5,Cocoon Gem - 嵌套窗體內嵌套形式

我有組織,包:: Bip和男高音模型。

協會是:

組織

has_many :bips, as: :ipable, class_name: Package::Bip 
    accepts_nested_attributes_for :bips, reject_if: :all_blank, allow_destroy: true 

套票:Bip的(多態)

belongs_to :ipable, :polymorphic => true, optional: true, inverse_of: :bip 

    has_one :tenor, as: :tenor 
    accepts_nested_attributes_for :tenor, reject_if: :all_blank, allow_destroy: true 

男高音(多態)

belongs_to :tenorable, :polymorphic => true, optional: true 

種的形式有:

在我的組織/ _form.html.erb,我有:

<%= f.simple_fields_for :bips do |f| %> 
     <%= f.error_notification %> 
     <%= render 'package/bips/bip_fields', f: f %> 

    <% end %> 

    <%= link_to_add_association 'Add another intellectual property resource', f, :bips, partial: 'package/bips/bip_fields' %> 

在我bip_fields.html.erb嵌套形式,我有:

<%# if @package_bips.tenor.blank? %> 
    <%= link_to_add_association 'Add timing', f, :tenor, partial: 'tenors/tenor_fields' %> 
<%# end %> 

<%= f.simple_fields_for :tenor do |tenor_form| %> 
    <%= f.error_notification %> 
    <%= render 'tenors/tenor_fields', f: tenor_form %> 
<% end %> 

Javascript

cocoon docs建議添加一個js文件來指定關聯入口作爲一個功能。在我tenor_subform.js我:

$(document).ready(function() { 
    $(".add_tenor a"). 
     data("association-insertion-method", 'append'). 
     data("association-insertion-node", function(link){ 
     return link.closest('.row').next('.row').find('.tenor_form') 
     }); 
}); 

控制器

在我的組織控制,我有:

def new 
    @organisation = Organisation.new 
    @organisation.bips 
end 

:我不知道,如果我需要將另一行添加到我的新操作中以創建organisation.bip.tenor實例。我也不確定是否應該通過關於引導男高音的organisation.rb的關聯添加has_one。

def organisation_params 
     params.fetch(:organisation, {}).permit(:title, :comment, 

      bips_attributes:   [:id, :status, :_destroy, 
      tenor_attributes:   [:id,:commencement, :expiry,      :_destroy] 

     ], 

在我的男高音控制器,我有:

def tenor_params 
     params.require(:tenor).permit(:commencement, :expiry) 
    end 

錯誤

我不知道我是否需要添加男高音行動組織控制器(BIP的最終母公司後者又是男高音的父母)。

當我保存了這一切,並嘗試它,我得到一個錯誤,指出:

unknown attribute 'tenor_id' for Tenor. 

當我看到其他與此錯誤SO崗位,其原因往往是:id屬性還沒有被列入白名單在父類中。我已經做到了。

任何人都可以看到我做錯了什麼嗎?

男高音控制器

class TenorsController < ApplicationController 

    before_action :set_tenor, only: [:show, :edit, :update, :destroy] 
    before_action :authenticate_user! 
    # after_action :verify_authorized 

    def index 
    @tenors = Tenor.all 
    # authorize @tenors 
    end 

    def show 

    end 

    def new 
    @tenor = Tenor.new 
    # authorize @tenor 
    end 

    def edit 

    end 

    def create 
    @tenor = Tenor.new(tenor_params) 
    # authorize @tenor 

    respond_to do |format| 
     if @tenor.save 
     format.html { redirect_to @tenor } 
     format.json { render :show, status: :created, location: @tenor } 
     else 
     format.html { render :new } 
     format.json { render json: @tenor.errors, status: :unprocessable_entity } 
     end 
    end 
    end 

    def update 
    respond_to do |format| 
     if @tenor.update(tenor_params) 
     format.html { redirect_to @tenor } 
     format.json { render :show, status: :ok, location: @tenor } 
     else 
     format.html { render :edit } 
     format.json { render json: @tenor.errors, status: :unprocessable_entity } 
     end 
    end 
    end 

    def destroy 
    @tenor.destroy 
    respond_to do |format| 
     format.html { redirect_to action: :index } 
     format.json { head :no_content } 
    end 
    end 

    private 
    def set_tenor 
     @tenor = Tenor.find(params[:id]) 
     # authorize @tenor 
    end 

    def tenor_params 
     params.require(:tenor).permit(:express_interest, :commencement, :expiry, :enduring, :repeat, :frequency) 
    end 

end 
+0

該錯誤提示你的地方你指的是一個'tenor_id'了'Tenor'對象。我們沒有看到足夠的代碼來查看它的位置。你的男高音應該有bips_id,但既然你有'has_one',那麼根本就不應該有'tenor_id'? – nathanvda

+0

不確定你的意思。男高音是多形的,它將持有一個bips id。我不明白這個建議。你的意思是最終的父母(organisation.rb)也應該與Tenor有聯繫嗎? – Mel

+0

我的意思是:錯誤表示你正在嘗試分配一個男高音ID,所以你的代碼/表單可以在哪裏?你能告訴我們什麼是張貼到控制器? – nathanvda

回答

1

has_one關係錯誤聲明。因爲你說as: :tenor使它尋找tenor_id

你有如下聲明一下:

has_one :tenor, as: :tenorable 
+0

ahh - 當然。謝謝 – Mel

0

模型中沒有看到的nested_attr.Add :inverse_of => #{model}的ID。

實施例:

class Tenor < ActiveRecord::Base 
    has_many :traps, :inverse_of => :bips 
end 

有關詳細信息看thisthis文檔。

+0

但是tenor.rb是多態的,它有:belongs_to:tenorable,:polymorphic => true,可選:true – Mel