Mirza
Mirza

Reputation: 645

How to use ajax in ruby on rails to update a form after a field has been filled out

I have two models, one is called labor_report and the other is employee, My ruby on rails application deals with creating labor reports for a certain employee. So after the employee enters his employee number in a text field for the labor report, i need ajax to grab the corresponding name and department number from employees and fill in the other textboxes for the labor report that correspond to that data.

Upvotes: 0

Views: 1860

Answers (2)

Plattsy
Plattsy

Reputation: 515

If you are using jquery and unobtrusive javascript, you could use jquery's "blur", which triggers an event when you click or tab out of a text field:

$("input").blur(function(){
      $.ajax({
        type: "POST",
        url: "/controller/action",
        data: $(this).val(),
        dataType: "script",
        callback: null
      });
    }
    return false;
  });

Upvotes: 2

Shane T
Shane T

Reputation: 212

Maybe you're looking for observe_field.

http://apidock.com/rails/ActionView/Helpers/PrototypeHelper/observe_field

Upvotes: 2

Related Questions