Bring data automatically with Associations

1

I have two models cliente.rb and atendimento.rb , both already with their associations . When I am going to generate a new service with collection_select select the name of the client, the problem and what I would like at the time of selecting the client other fields like address, phone would also come automatically to my form, for example: in my register I have a name, a phone number, an address, etc. Already in my service registration I have the name of the client (I search through collection_select ), telephone of the client (I want it to be filled automatically when selecting the client), address, etc.

    
asked by anonymous 16.10.2014 / 22:01

1 answer

1

Next, let's break it down:

  • Choose to write your code in English, as you will experience problems with design. For example, the Distributor template in the plural will look like this: Distribuidors . What will compel you to write things like: empresa.distribuidors.each {} .
  • To make this autocomplete you should follow these steps:

    a) Create an endpoint that will respond to the GET method in json format, something like: /private_api/clientes.json , note that you should note authentication issues.

    b) By javascript when selecting a client in your service form, it should trigger a call to the endpoint and thus take the data and fill out the form. would be something like this in jQuery:

    $(document).ready(function() {
      $('#atendimento_customer_id').change(function() {
        $.getJSON('/private_api/clientes.json', function(data) {  
          $('.nome-do-field').val(date.nomeDoField)
        })
      })         
    }) 
    
  • 07.11.2014 / 20:29