模型“Formula”有许多“操作数”使用simple_nested_form创建,使用字段。似乎该过程是正确的,并且没有启动错误,但操作数未保存。
公式模型:
cellspacing
操作数型号:
class Formula < ActiveRecord::Base
attr_accessible :name, :client_id
validates_presence_of :name, :client_id
belongs_to :client
has_many :operands, as: :operation, dependent: :destroy
accepts_nested_attributes_for :operands, allow_destroy: true
attr_accessor :operator, :numeric_operand, :operation_id, :operation_type
end
公式控制器:
class Operand < ActiveRecord::Base
attr_accessible :operator, :numeric_operand, :operation_id, :operation_type
validates_presence_of :operator
belongs_to :operation, polymorphic: true
OPERATOR_TYPES = ["+", "-", "*", "/"]
end
公式_form:
class FormulasController < ApplicationController
load_and_authorize_resource
def new
@formula.operands.build
end
def create
@formula = Formula.new(params[:formula])
@formula.client_id = @current_client.id unless @formula.client_id.present?
if @formula.save
redirect_to @formula, notice: t('controllers.notice.successfully_created') }
else
render action: "new"
end
end
def edit
end
def update
if @formula.update_attributes(params[:formula])
redirect_to @formula, notice: t('controllers.notice.successfully_updated')
else
render action: "edit"
end
end
_operands_fields:
= simple_nested_form_for @formula, :html => { :class => 'form-vertical' } do |f|
= f.error_notification
.form-inputs
.row
.span3= f.input :name
.well
= f.fields_for :operands
= f.link_to_add t('helpers.links.add'), :operands
.form-actions
= f.button :submit, :class => 'btn-primary', disable_with: "#{t('helpers.links.submitting')}"
这一切看起来都很好,但是当进程结束时,不会创建任何操作数。在更新中检查返回的params或使用更好的错误创建方法时,可以从表单中成功检索操作数属性,但不会创建公式与它们之间的关系:
.well
.row
.span2= f.input :operator, collection: Operand::OPERATOR_TYPES
.span3= f.input :numeric_operand
答案 0 :(得分:1)
继续我们的讨论<div class="checkbox">
<label><input type="checkbox" value="mapDataLayer" checked>
Option 1</label>
</div>
不适用于Rails 4中的多态关联的嵌套属性(未经验证),然后我建议使用强参数:
protected_attributes