REST with problem in CREATE

Asked

Viewed 152 times

1

Introducing

I am developing an app and using Rails.

$ rake routes
POST   /companies(.:format)        companies#create

Routes.Rb

resources :companies

Problem

When I step my object to be inserted using Postman

{"name":"Campo 1","brand_name":"Campo 2", "tax_id":"Campo 3","business_area":"Campo 4","phone":"Campo 5","email":"Campo 6","zip_code":"Campo 7","address":"Campo 8","neighborhood":"Campo 9","city":"Campo 10","state":"Campo 11","agent_name":"Campo 12","agent_tax_id":"Campo 13","operator_name":"Campo 14","operator_tax_id":"Campo 15","company_type":"Campo 16","inscricao_estadual":"Campo 17","inscricao_municipal":"Campo 18"}

He brings me like Sponse:

{"name":["Campo obrigatório"],"brand_name":["Campo obrigatório"],"tax_id":["Campo obrigatório"],"business_area":["Campo obrigatório"],"phone":["Campo obrigatório"],"email":["Campo obrigatório"],"zip_code":["Campo obrigatório"],"address":["Campo obrigatório"],"neighborhood":["Campo obrigatório"],"city":["Campo obrigatório"],"state":["Campo obrigatório"],"agent_name":["Campo obrigatório"],"agent_tax_id":["Campo obrigatório"],"operator_name":["Campo obrigatório"],"operator_tax_id":["Campo obrigatório"],"company_type":["Campo obrigatório"],"inscricao_estadual":["Campo obrigatório"],"inscricao_municipal":["Campo obrigatório"]}

Postman inserir a descrição da imagem aqui

Companies_controller.Rb

class CompaniesController < ApplicationController
  before_action :set_company, :only => [:show, :update, :destroy]
  skip_before_action  :verify_authenticity_token, :only => [:create, :destroy]
  # protect_from_forgery  except :create

  def index
    render :json =>  Company.all
  end

  def show
    render :json => @company, :methods => [:factor_ids, :daily_limit]
  end

  def create
    data = @company_params
    @company = Company.new data

    Company.transaction do
      if @company.save
        password = SecureRandom.hex(16)
        @user = User.new({
          :email => @company.email,
          :company => @company,
          :password => password,
          :password_confirmation => password
        })

        @user.save
        @user.send_password_reset
        render :json => @company and return
      end

      render :json => @company.errors, :status => 422
      raise ActiveRecord::Rollback
    end
  end

  def destroy
    if @company.destroy
      head :no_content
    else
      render :json 
    end
  end

  def update
    if @company.update company_params
      head :no_content
    else
      render :json => @company.errors, :status => :unprocessable_entry
    end
  end

  def company_params
    hsh = params[:company].permit(:name, :brand_name, :company_type, :tax_id, :inscricao_estadual, :inscricao_estadual_isento, :inscricao_municipal, :inscricao_municipal_isento, :business_area, :phone, :address, :address_number, :address_complement, :neighborhood, :city, :state, :zip_code, :agent_name, :agent_tax_id, :operator_name, :operator_tax_id, :email, :email_confirmation, :status)
    hsh
  end

  private
  def set_company
    @company = Company.find(params[:id])    
  end
end

1 answer

3


Rails expects you to post in the format:

{
    "company":
    {
        "name": "teste",
        "email": "[email protected]"
    }
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.