Currently, I am experimenting with the cocoon gem to construct nested forms efficiently.
Within my application, I have defined models for Organisation, Package::Bip, and Tenor.
The relationships between these models are as follows:
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 setup includes:
In my organisations/_form.html.erb file, it is structured as:
<%= 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' %>
Within the nested form bip_fields.html.erb, the configuration includes:
<%# 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 Integration
For handling associations, a specific JavaScript file is necessary according to cocoon documentation. In my tenor_subform.js file, the implementation looks like this:
$(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')
});
});
Controller Side
Within the organisation controller, the method setup includes:
def new
@organisation = Organisation.new
@organisation.bips
end
Note: There is a doubt regarding the necessity of adding an extra line in the new action to establish the organisation.bip.tenor instance. Additionally, uncertainty arises concerning the imperative of adding a has_one through association in organisation.rb that points to tenor.
def organisation_params
params.fetch(:organisation, {}).permit(:title, :comment,
bips_attributes: [:id, :status, :_destroy,
tenor_attributes: [:id,:commencement, :expiry, :_destroy]
],
The tenor controller has the following configuration:
def tenor_params
params.require(:tenor).permit( :commencement, :expiry)
end
Error Analysis
Queries arise regarding the inclusion of tenor actions in the organisation controller, acting as the parent of bip, which is the parent of tenor.
Upon executing the setup and attempting to proceed, a specific error emerges:
unknown attribute 'tenor_id' for Tenor.
When researching similar errors on Stack Overflow, the recurring cause often relates to the lack of whitelisting the :id attribute in the parent class. Despite addressing this concern, the error persists.
Is there anyone who can pinpoint the flaw in my implementation?
Tenor Controller Overview
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