我与Cocoon gem
有一个奇怪的问题,每当我通过Cocoon添加新字段时,这些字段都不会以params hash发送。
常规字段,不是从cocoon构建的字段按预期工作。这是相关代码。
class Standard < ActiveRecord::Base
has_many :spaces, as: :space_sourceable
accepts_nested_attributes_for :spaces, :reject_if => :all_blank, :allow_destroy => true
end
class Space < ActiveRecord::Base
belongs_to :space_sourceable, polymorphic: true
end
class ContractsController < ApplicationController
def create
set_resource(resource_class.new resource_params)
if get_resource.save
redirect_to authenticated_root_url, notice: "Contract successfully created."
else
render "new"
end
end
def new
set_resource(resource_class.new)
build_nested_resources
end
private
def build_nested_resources
resource.spaces.build
end
def get_resource
instance_variable_get("@#{resource_name}")
end
def set_resource(resource = nil)
resource ||= resource_class.find_by_id params[:id]
instance_variable_set("@#{resource_name}", resource)
failure unless resource
end
def resource_name
@resource_name ||= self.controller_name.singularize
end
def resource_class
@resource_class ||= resource_name.classify.constantize
end
def resource_params
params.require(resource_name.to_sym).permit(:id, :name, :note, spaces_attributes: [:id, :unit, :building, :floor, :amendment_type, :area, :space_sourceable_type, :space_sourceable_id, :_destroy]
end
end
class StandardsController < ContractsController
end
#_form.html.haml
= form_for resource do |f|
- if resource.errors.any?
#error_explanation
%h2
= pluralize(resource.errors.count, "error")
prohibited this lease information from being saved:
%ul
- resource.errors.full_messages.each do |msg|
%li= msg
.field
%strong.upper Name:
= f.text_field :name, class: "input", style: "width: 450px;", required: true
%h3.center-heading Space
= render partial: "spaces", locals: {f: f}
#_spaces.html.haml
= f.fields_for :spaces do |space|
= render "space_fields", f: space
.links
= link_to_add_association 'Add Space', f, :spaces, partial: "space_fields"
#_space_fields.html.haml
.nested-fields
.field
.upper Unit
= f.text_field :unit, class: "input", style: "width: 450px;"
.field
.upper Building
= f.text_field :building, class: "input", style: "width: 450px;"
.field
.upper Floor
= f.text_field :floor, class: "input", style: "width: 450px;"
.field
.upper Area
= f.text_field :area, class: "input", style: "width: 450px;"
.field
.upper Amendment Type
= f.text_field :amendment_type, class: "input", style: "width: 450px;"
= link_to_remove_association "Remove this Space", f
由Cocoon和普通表单字段生成的HTML看起来也很好
#normal field from form
<input type="text" id="standard_spaces_attributes_0_unit" name="standard[spaces_attributes][0][unit]" class="input">
#field generated by cocoon
<input type="text" id="standard_spaces_attributes_1436417485840_unit" name="standard[spaces_attributes][1436417485840][unit]" class="input">
这是将以下参数发送到服务器
{"standard"=>{"name"=>"fdfdfd", "spaces_attributes"=>{"0"=>{"unit"=>"asds", "building"=>"", "floor"=>"", "area"=>"", "amendment_type"=>""}}, "note"=>""}, "commit"=>"Create Standard", "utf8"=>"✓", "authenticity_token"=>"MYAn/MTgEOpS5B0xubsmMEW53EbOSPkgvJ/1sWTJAVcIf8cZLMNI6O1Py3qBho4Kkgteq5smJyYBNH1GT26Bqw==", "controller"=>"standards", "action"=>"create"}
P.S set_resource和get_resource或任何其他通用方法按预期工作,因为我能够在正常情况下保存值。我也可以通过cocoon添加和删除字段,只是这些字段不与其他表单属性一起发送。它也不是一个强大的参数问题,因为只有当它们实际存在于params散列中时,值才会被列入白名单。