Rails 5, Cocoon Gem - nested form inside nested form

孤街浪徒 提交于 2019-12-24 06:37:37

问题


I am trying to use cocoon gem to build nested forms.

I have models for Organisation, Package::Bip and Tenor.

The associations are:

Organisation

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

Package::Bip (polymorphic)

 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

Tenor (polymorphic)

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

The forms have:

In my organisations/_form.html.erb, I have:

<%= 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' %> 

In my bip_fields.html.erb nested form, I have:

<%# 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

The cocoon docs suggest adding a js file to specify association-insertion-node as a function. In my tenor_subform.js I have:

$(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')
      });
});

Controllers

In my organisation controller, I have:

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

Note: I'm not sure if I need to add another line to my new action to create the organisation.bip.tenor instance. I'm also unsure if im supposed to add has_one through association on the organisation.rb that references the tenor.

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

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

        ],

In my tenor controller, I have:

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

ERRORS

I am not sure if I need to add tenor actions to the organisation controller (the ultimate parent of bip which in turn is the parent of tenor).

When I save all of this and try it, I get an error that says:

unknown attribute 'tenor_id' for Tenor.

When I see other SO posts with this error, its often because the :id attribute hasn't been whitelisted in the parent class. I have done that.

Can anyone see what I've done wrong?

Tenor controller

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

回答1:


You has_one relation is wrongly declared. Because you say as: :tenor makes it look for a tenor_id.

You have to declare it as follows:

has_one :tenor, as: :tenorable 



回答2:


Your model does not see the id of nested_attr.Add :inverse_of => #{model}.

Example:

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

For more information look this or this documentation.



来源:https://stackoverflow.com/questions/40413622/rails-5-cocoon-gem-nested-form-inside-nested-form

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!