Automatically bring data with Associations

Asked

Viewed 225 times

1

I have two models cliente.rb and atendimento.rb, both already with their Associations. When will I generate a new service with you through collection_select select the name of the customer, the problem and that I would like to select the client when selecting other fields like address, phone also automatically come to my form, for example: in my customer registration I have name, phone, address, etc. Already in my service registration I have the name of the customer (I search through the collection_select), customer’s phone (I want it to be filled in automatically when selecting the customer), address, etc.

1 answer

1

Next, let’s go by part:

  1. Choose to write your code in English as you will face design problems. For example: the model Distributor plural will look like this: Distribuidors. Which will make you write things like: empresa.distribuidors.each {}.
  2. To do this auto-fill you must follow the following steps:

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

    b) By javascript when selecting a customer on your customer support form, they must trigger a call to endpoint and then take the data and fill out the form. would be something like that in jQuery:

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

Browser other questions tagged

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