方法更新和错误unpermited参数rails 4

时间:2016-05-14 18:47:52

标签: ruby-on-rails angularjs nested-attributes strong-parameters

我在rails 4和angularjs中遇到强参数问题,在控制器内部的方法更新中我收到了以下错误:

未经许可的参数:id,cliente_id,created_at,updated_at 未允许的参数:id,cliente_id,created_at,updated_at 未允许的参数:id,cliente_id,created_at,updated_at 未允许的参数:id,cliente_id,created_at,updated_at 未允许的参数:id,cliente_id,created_at,updated_at 未允许的参数:id,cliente_id,created_at,updated_at 未允许的参数:id,cliente_id,created_at,updated_at 未允许的参数:id,format,cliente

这是我的json发送的:

    {
    "id"=>"52", 
    "CliERP"=>"C-00125", 
    "CliRazao"=>"CHOCOLANDIA COMERCIO DE CHOCOLATES LTDA.", 
    "CliCNPJ"=>"55946756000110", 
    "CliEmail"=>"contato@chocolandia.com.br", 
    "CliObs"=>"TESTE\nTESTE CHOCOMANIA 123456", 
    "enderecos_attributes"=>[
        {
            "id"=>52, 
            "cliente_id"=>52, 
            "EndTipo"=>"E", 
            "EndCEP"=>"78456441", 
            "EndLogradouro"=>"RUA IPIRANGA", 
            "EndNumero"=>"2500", 
            "EndBairro"=>"CENTRO", 
            "EndCidade"=>"PORTO ALEGRE", 
            "EndEstado"=>"RS", 
            "created_at"=>"2016-04-30T18:00:11.249Z", 
            "updated_at"=>"2016-04-30T18:00:11.249Z"
        }
    ], 
    "telefones_attributes"=>[
        {
            "id"=>3, 
            "cliente_id"=>52, 
            "TelTipo"=>"F", 
            "TelNumero"=>"7843257896", 
            "created_at"=>"2016-04-30T18:00:11.251Z", 
            "updated_at"=>"2016-04-30T18:00:11.251Z"
        }, 
        {
            "id"=>4, 
            "cliente_id"=>52, 
            "TelTipo"=>"F", 
            "TelNumero"=>"7845214563", 
            "created_at"=>"2016-04-30T18:00:11.254Z", 
            "updated_at"=>"2016-04-30T18:00:11.254Z"
        }, 
        {
            "id"=>5, 
            "cliente_id"=>52, 
            "TelTipo"=>"C", 
            "TelNumero"=>"78996568941", 
            "created_at"=>"2016-04-30T18:00:11.257Z", 
            "updated_at"=>"2016-04-30T18:00:11.257Z"
        }
    ], 
    "emails_attributes"=>[
        {
            "id"=>1, 
            "cliente_id"=>52, 
            "MailTipo"=>"E", 
            "MailEndereco"=>"financeiro@chocolandia.com.br", 
            "created_at"=>"2016-04-30T18:00:11.261Z", 
            "updated_at"=>"2016-04-30T18:00:11.261Z"
        }, 
        {
            "id"=>2, 
            "cliente_id"=>52, 
            "MailTipo"=>"E", 
            "MailEndereco"=>"cristina.presidente@chocolandia.com.br", 
            "created_at"=>"2016-04-30T18:00:11.275Z", 
            "updated_at"=>"2016-04-30T18:00:11.275Z"
        }, 
        {
            "id"=>3, 
            "cliente_id"=>52, 
            "MailTipo"=>"E", 
            "MailEndereco"=>"roberto.comercial@chocolandia.com.br", 
            "created_at"=>"2016-04-30T18:00:11.277Z", 
            "updated_at"=>"2016-04-30T18:00:11.277Z"
        }
    ]
}

这是我的控制者:

class ClientesController < ApplicationController
    def index
        @clientes = Cliente.all
    end

    def show        
        @clientes = Cliente.find(params[:id])
        @clientes.enderecos = Endereco.where("cliente_id = ?", params[:id])
        @clientes.telefones = Telefone.where("cliente_id = ?", params[:id])
        @clientes.emails = Email.where("cliente_id = ?", params[:id])
        #logger.debug "RETORNO => #{@clientes.inspect}"
    end

    def create
        @cliente = Cliente.new(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
        respond_to do |format|
            if @cliente.update(cliente_params)
                format.json { render :show, status: :ok }
            else
                format.json { render json: @cliente.errors, status: :unprocessable_entity }
            end
        end
    end

    def destroy
        @cliente.destroy
        respond_to do |format|
            format.json {head :no_content}
        end
    end

    private
    def cliente_params
        params.permit(:CliERP, :CliRazao, :CliCNPJ, :CliEmail, :CliObs, 
            enderecos_attributes: [:EndTipo, :EndLogradouro, :EndNumero, :EndBairro, :EndCidade, :EndCEP, :EndEstado],
            telefones_attributes: [:TelTipo, :TelNumero],
            emails_attributes: [:MailTipo, :MailEndereco])
    end
end

编辑:这是我的模型&#39>

cliente.rb

class Cliente < ActiveRecord::Base
    has_many :enderecos, autosave: true
    has_many :telefones, autosave: true
    has_many :emails, autosave: true
    has_many :pedido

    accepts_nested_attributes_for :enderecos
    accepts_nested_attributes_for :telefones
    accepts_nested_attributes_for :emails
end

endereco.rb

class Endereco < ActiveRecord::Base
    belongs_to :cliente
end

email.rb

class Email < ActiveRecord::Base
    belongs_to :cliente
end

telefone.rb

class Telefone < ActiveRecord::Base
    belongs_to :cliente
end

我认为我的问题出在client_params中 谢谢!

1 个答案:

答案 0 :(得分:0)