Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/52.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails Cocoon有许多多态关联,不在参数中发送表单值_Ruby On Rails_Ruby On Rails 4_Cocoon Gem - Fatal编程技术网

Ruby on rails Cocoon有许多多态关联,不在参数中发送表单值

Ruby on rails Cocoon有许多多态关联,不在参数中发送表单值,ruby-on-rails,ruby-on-rails-4,cocoon-gem,Ruby On Rails,Ruby On Rails 4,Cocoon Gem,我有一个奇怪的问题,每当我通过Cocoon添加新字段时,这些字段都不会发送到params散列中 正常的领域,那些不是建立在茧的预期工作。这是相关代码 class Standard < ActiveRecord::Base has_many :spaces, as: :space_sourceable accepts_nested_attributes_for :spaces, :reject_if => :all_blank, :allow_destroy => tru

我有一个奇怪的问题,每当我通过Cocoon添加新字段时,这些字段都不会发送到params散列中

正常的领域,那些不是建立在茧的预期工作。这是相关代码

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

p.S set_resource和get_resource或任何其他通用方法按预期工作,因为我能够在正常情况下保存值。我还可以通过cocoon添加和删除字段,只是这些字段没有与其他表单属性一起发送。这也不是一个很强的参数问题,因为只有在参数散列中实际存在的值才会被列入白名单。

您能发布控制器的相关
新方法吗?@Pavan我试图避免编写所有代码,因为这部分工作正常。不管怎样,我已经把它包括进去了。您是否在
构建嵌套资源
中获得了正确的
资源
?@Pavan是的,它显示在params散列中,并保存在db中。只有cocoon生成的字段不会以paramsBit形式发送。cocoon生成的字段是什么?您能发布控制器的相关
新方法吗?@Pavan我试图避免编写所有代码,因为该部分工作正常。不管怎样,我已经把它包括进去了。您是否在
构建嵌套资源
中获得了正确的
资源
?@Pavan是的,它显示在params散列中,并保存在db中。只有cocoon生成的字段不会以paramsBit形式发送。蚕茧产生的能量场是什么?
#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"}