How to autocomplete fields when selecting a code - Ruby On Rails

0

Greetings! I have a sales form, in which I need that when writing the product code within its corresponding field, the fields, name and price, are automatically completed with the corresponding information that is in the database, and that they belong to that code, but to be honest, I'm not sure how to achieve it, I know you would have to achieve more with the part of js, I appreciate your help, this would be the form and my migration:

product_form.html.erb

<%= form_with(model: product, local: true) do |form| %>

  <div class="field">
    <%= form.label :code %>
    <%= form.text_field :code %>
  </div>

  <div class="field">
    <%= form.label :name %>
    <%= form.text_field :name %>
  </div>

  <div class="field">
    <%= form.label :price %>
    <%= form.text_field :price %>
  </div>

  <div class="actions">
    <%= form.submit %>
  </div>
<% end %>

Product.rb

class CreateProducts < ActiveRecord::Migration[5.2]
  def change
    create_table :products do |t|
      t.string :code
      t.string :name
      t.float :price

      t.timestamps
    end
  end
end

I appreciate immensely someone could get me out of this quagmire!

    
asked by Hector Hernandez 27.08.2018 в 02:09
source

2 answers

0

For this functionality you need to use javascript and ruby, select2 for when you type a letter you can go to the database and bring the values you need, likewise you will need an endpoint /model.json of where your data will be reading, the implementation is very simple.

    
answered by 02.09.2018 в 03:59
0

Actually what you want to do is simple. I write you in steps what you want to do

  • Capture the event with jquery or ES6. (You can use keyDown or keyUp or change)

  • It is to create a route that looks for this code.

  • Your controller must return a json with the answer that are the price and the name.

  • When requesting via ajax in your callback success or how you handle it, you fill in the information in the inputs

  • answered by 26.09.2018 в 08:21