我想用茧宝石来构建嵌套窗体。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
该错误提示你的地方你指的是一个'tenor_id'了'Tenor'对象。我们没有看到足够的代码来查看它的位置。你的男高音应该有bips_id,但既然你有'has_one',那么根本就不应该有'tenor_id'? – nathanvda
不确定你的意思。男高音是多形的,它将持有一个bips id。我不明白这个建议。你的意思是最终的父母(organisation.rb)也应该与Tenor有联系吗? – Mel
我的意思是:错误表示你正在尝试分配一个男高音ID,所以你的代码/表单可以在哪里?你能告诉我们什么是张贴到控制器? – nathanvda