gpt4 book ai didi

javascript - Rails 5,Cocoon Gem - 嵌套形式中的嵌套形式

转载 作者:太空宇宙 更新时间:2023-11-04 16:23:32 25 4
gpt4 key购买 nike

我正在尝试使用 cocoon gem 来构建嵌套表单。

我有 Organization、Package::Bip 和 Tenor 的模型。

协会是:

组织

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 文档建议添加一个 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')
});
});

Controller

在我的组织 Controller 中,我有:

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

注意:我不确定是否需要在新操作中添加另一行来创建organization.bip.tenor 实例。我也不确定我是否应该通过引用男高音的organization.rb上的关联添加has_one。

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

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

],

在我的男高音 Controller 中,我有:

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

错误

我不确定是否需要将 tenor 操作添加到组织 Controller (bip 的最终父级,而 bip 又是 tenor 的父级)。

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

unknown attribute 'tenor_id' for Tenor.

当我看到其他 SO 帖子出现此错误时,通常是因为 :id 属性尚未在父类中列入白名单。我已经这么做了。

有人能看出我做错了什么吗?

次中音 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

最佳答案

您的has_one关系声明错误。因为您说 as: :tenor 会使其查找 tenor_id

您必须按如下方式声明它:

has_one :tenor, as: :tenorable 

关于javascript - Rails 5,Cocoon Gem - 嵌套形式中的嵌套形式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40413622/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com