嵌套属性未插入 table Rails 和 AngularJS
Nested attributes not being inserted into table Rails and AngularJS
我在 Rails 4 上的 Ruby 有一个问题,方法创建和强参数,当发送带有嵌套值的 post 时,我的应用保存 object父亲正确,但 children 仅保存 create_at 和 update_at,在我的代码下方:
我的JSON发送
{
"CliRazao"=>"FORNECEDOR 1.",
"CliCNPJ"=>"78456896000185",
"CliEmail"=>"contato@contato.com.br",
"CliObs"=>"Teste\nTeste\nTEste",
"enderecos"=>[
{
"EndCEP"=>"17456789",
"EndTipo"=>"E",
"EndLogradouro"=>"RUA DOS TUCUNARES",
"EndNumero"=>"78",
"EndBairro"=>"JARDIM AQUARIUS",
"EndCidade"=>"MARILIA",
"EndEstado"=>"SP"
},
{
"EndCEP"=>"18456123",
"EndTipo"=>"C",
"EndLogradouro"=>"RUA AFONSO PENA",
"EndNumero"=>"78",
"EndBairro"=>"JARDIM NOVO MUNDO",
"EndCidade"=>"MARILIA",
"EndEstado"=>"SP"
}
],
"CliERP"=>"C-00125",
"cliente"=>
{
"CliERP"=>"C-00125",
"CliRazao"=>"FORNECEDOR 1",
"CliCNPJ"=>"78456896000185",
"CliEmail"=>"contato@contato.com.br",
"CliObs"=>"Teste\nTeste\nTEste"
}
}
我的模特cliente.rb(父亲)
class 客户 < ActiveRecord::Base
has_many:enderecos, 自动保存: true
accepts_nested_attributes_for :enderecos
结束
我的模型endereco.rb (Child)
class 恩德雷科 < ActiveRecord::Base
belongs_to:客户
结束
我的控制器
class ClientesController < ApplicationController
定义索引
@clientes = Cliente.all
结束
def findById
@clientes = Cliente.find(params[:id]);
end
def create
@cliente = Cliente.new(cliente_params);
@cliente.enderecos.build(cliente_params[:enderecos_attributes]);
logger.debug "Enderecos => #{cliente_params}"
respond_to do |format|
if @cliente.save
format.json { render :show, status: :created }
else
format.json { render json: @cliente.errors, status: :unprocessable_entity }
end
end
end
def update
end
private
def cliente_params
params.require(:cliente).permit(:CliERP, :CliRazao, :CliCNPJ, :CliEmail, :CliObs, enderecos_attributes: [:EndTipo, :EndLogradouro, :EndNumero, :EndBairro, :EndCidade, :EndCEP, :EndEstado])
end
结束
enderecos 没有插入到我的数据库中,也没有抛出异常。
谢谢!
您的属性实际上并未嵌套。如果它们是 enderecos 的 json
,那么它们将在客户内部。
对于嵌套属性,您需要几样东西。
你需要 accepts_nested_attributes_for
,你有。
在我们的表单中,您需要对嵌套属性使用 fields_for
在您的新操作中,您需要 build
嵌套资源
在您的控制器中,您需要使用 .permit(:enderecos_attributes => [:attribute_name, :another_attribute_name]
将嵌套属性添加到允许的参数中
希望对您有所帮助。
我在 Rails 4 上的 Ruby 有一个问题,方法创建和强参数,当发送带有嵌套值的 post 时,我的应用保存 object父亲正确,但 children 仅保存 create_at 和 update_at,在我的代码下方:
我的JSON发送
{ "CliRazao"=>"FORNECEDOR 1.", "CliCNPJ"=>"78456896000185", "CliEmail"=>"contato@contato.com.br", "CliObs"=>"Teste\nTeste\nTEste", "enderecos"=>[ { "EndCEP"=>"17456789", "EndTipo"=>"E", "EndLogradouro"=>"RUA DOS TUCUNARES", "EndNumero"=>"78", "EndBairro"=>"JARDIM AQUARIUS", "EndCidade"=>"MARILIA", "EndEstado"=>"SP" }, { "EndCEP"=>"18456123", "EndTipo"=>"C", "EndLogradouro"=>"RUA AFONSO PENA", "EndNumero"=>"78", "EndBairro"=>"JARDIM NOVO MUNDO", "EndCidade"=>"MARILIA", "EndEstado"=>"SP" } ], "CliERP"=>"C-00125", "cliente"=> { "CliERP"=>"C-00125", "CliRazao"=>"FORNECEDOR 1", "CliCNPJ"=>"78456896000185", "CliEmail"=>"contato@contato.com.br", "CliObs"=>"Teste\nTeste\nTEste" }
}
我的模特cliente.rb(父亲)
class 客户 < ActiveRecord::Base has_many:enderecos, 自动保存: true
accepts_nested_attributes_for :enderecos
结束
我的模型endereco.rb (Child)
class 恩德雷科 < ActiveRecord::Base belongs_to:客户 结束
我的控制器
class ClientesController < ApplicationController 定义索引 @clientes = Cliente.all 结束
def findById @clientes = Cliente.find(params[:id]); end def create @cliente = Cliente.new(cliente_params); @cliente.enderecos.build(cliente_params[:enderecos_attributes]); logger.debug "Enderecos => #{cliente_params}" respond_to do |format| if @cliente.save format.json { render :show, status: :created } else format.json { render json: @cliente.errors, status: :unprocessable_entity } end end end def update end private def cliente_params params.require(:cliente).permit(:CliERP, :CliRazao, :CliCNPJ, :CliEmail, :CliObs, enderecos_attributes: [:EndTipo, :EndLogradouro, :EndNumero, :EndBairro, :EndCidade, :EndCEP, :EndEstado]) end
结束
enderecos 没有插入到我的数据库中,也没有抛出异常。
谢谢!
您的属性实际上并未嵌套。如果它们是 enderecos 的 json
,那么它们将在客户内部。
对于嵌套属性,您需要几样东西。
你需要
accepts_nested_attributes_for
,你有。在我们的表单中,您需要对嵌套属性使用
fields_for
在您的新操作中,您需要
build
嵌套资源在您的控制器中,您需要使用
.permit(:enderecos_attributes => [:attribute_name, :another_attribute_name]
将嵌套属性添加到允许的参数中
希望对您有所帮助。