- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我正在尝试使用 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/
我正在寻找一种方法来进行更复杂的确认,确认我现在使用的是这样的: ".html_safe, p, :class => 'btn-link remove has-tooltip',
我一直在开发一个跟踪基本事件的 Rails 应用程序,我希望扩展它(这是我自己的生活数据跟踪应用程序)。我有一个 EventTemplate 类,它存储 EventTemplateAttributes
我有一个使用 Cocoon gem 生成的 lineItem 字段,在尝试保存行项目字段值时遇到问题,并且当我检查元素时,add_item 按钮没有为生成的每个新字段触发唯一 ID。 class
我在 ruby make 和 cocoon 中有一个函数嵌套形式。问题是我尝试使用 before-insert 和 after-insert 但它什么也没做。 我的喵:
我有两个 Cocoon 网站:ABC 和 ABC-mobile。 如果用户来自移动浏览器,那么我希望他们重定向到 ABC-mobile。我该如何实现这个目标? 最佳答案 您需要检测用户代理。在 Coc
我几乎完成了一个使用 cocoon 的表单,该表单管理需要动态生成表单的各个部分的深度嵌套表单。数据库设置为 Visualizations has_many Rows,Rows has_many pa
我在渲染嵌套字段中的选项集合时遇到问题。该集合取决于现有表单中填写的值。 我相信修复方法在于为每个新的嵌套输入字段指定一个具有唯一 ID 的新输入字段,但如果我错了,请纠正我。 上下文 渲染订单表单时
在一个 Rails 项目中,我使用 cocoon gem创建嵌套表单。在provided example ,方法 link_to_remove_association 被使用,生成一个链接,点击时删除
根据https://github.com/nathanvda/cocoon#link_to_add_association您应该能够将函数传递给 data-association-insertion-
我正在尝试将 cocoon 用于嵌套的 ajax 表单和 formtastic 我的导轨版本 Rails 3.2.3 我已经把 gem "cocoon"放在了我的 gemfile 中,并进行了捆绑安
我一直在使用 Cocoon gem 在 rails 中动态生成嵌套字段。我遇到了一个应用程序,我想用数字标记 cocoon 生成的字段;类似如下。 Field 1: __________ Field
我有使用嵌套表单的 cocoon,如果您单击添加字段链接,它会插入输入字段。如何自动呈现第一个输入,然后在单击“添加字段”时插入其他输入? 最佳答案 在您的 Controller 中,使用此代码。在下
我们研究了从经典的 ASP 迁移到基于 Java 的 Web 应用程序(即 JavaServer Faces 2.0)的可能性。旧的 ASP 应用程序使用 XSLT 来呈现基本的 UI 组件,我们希望
我正在构建一个带有嵌套表单的应用程序,并希望使用 jquery 动态添加字段。 我听人们谈论过两个 gem 。一个是nested_form_fields,另一个是cocoon。 有人比较过这两种 ge
我有一个带有 cocoon 的嵌套表单,但在尝试从 JavaScript 计算总价时遇到问题。当我检查查看源页面时,字段生成的属性没有值属性。如果我使用@invoice.line_items.buil
本文整理了Java中org.apache.cocoon.components.flow.WebContinuation类的一些代码示例,展示了WebContinuation类的具体用法。这些代码示例主
我搜索了堆栈溢出和谷歌无济于事。 我有一个人有_one next_of_kin 我可以创建一个带有嵌套表单(带有茧)的人,并且可以完美保存。出于某种原因,当我转到编辑页面时,它会删除关联的 next_
假设我有这些模型,我打算在同一个表单上添加/删除它们: class Survey < ActiveRecord::Base has_many :questions end class Questi
我正在使用 cocoon gem 处理动态嵌套表单。我有两个模型 class CrossTable @folders, :label_method => :title, :val
我目前有一个具有深度嵌套的复杂表单,并且我正在使用 Cocoon gem 根据需要动态添加部分(例如,如果用户想要在销售表单中添加另一辆车)。代码如下所示: "sale_vehicles/
我是一名优秀的程序员,十分优秀!