# Rails AJAX help

**URL:** <https://www.ruby-forum.com/t/rails-ajax-help/67514>\
**Category:** Ruby\
**Created:** [September 4, 2006, 8:52pm UTC](https://www.ruby-forum.com/t/rails-ajax-help/67514 "2006-09-04T20:52:12Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [September 4, 2006, 8:52pm UTC](https://www.ruby-forum.com/t/rails-ajax-help/67514/1 "2006-09-04T20:52:12Z")

</div>

I have an input, and I want to, when the user types a number on that  
input and exits it (the onBlur event), to search a table on that number  
and return a value to that input.

Thanks in advance.

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [September 4, 2006, 9:56pm UTC](https://www.ruby-forum.com/t/rails-ajax-help/67514/2 "2006-09-04T21:56:19Z")

</div>

removed\_email\_address@domain.invalid wrote:

> I have an input, and I want to, when the user types a number on that  
> input and exits it (the onBlur event), to search a table on that number  
> and return a value to that input.

Had you considered the idea of using the user’s entered number as an  
index  
into an array?

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [September 4, 2006, 10:14pm UTC](https://www.ruby-forum.com/t/rails-ajax-help/67514/3 "2006-09-04T22:14:21Z")

</div>

But my problem is not how to find the value, but the AJAX  
implementation in the onBlur event and how to return the value found  
back to the input.

Paul L. escreveu:

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [September 4, 2006, 10:26pm UTC](https://www.ruby-forum.com/t/rails-ajax-help/67514/4 "2006-09-04T22:26:22Z")

</div>

removed\_email\_address@domain.invalid wrote:

> But my problem is not how to find the value, but the AJAX  
> implementation in the onBlur event and how to return the value found  
> back to the input.

It would be very helpful if you were to post your code and ask specific  
questions about problems you are having with your code.

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [September 4, 2006, 10:44pm UTC](https://www.ruby-forum.com/t/rails-ajax-help/67514/5 "2006-09-04T22:44:23Z")

</div>

Paul L. wrote:

> removed\_email\_address@domain.invalid wrote:
> 
> > But my problem is not how to find the value, but the AJAX  
> > implementation in the onBlur event and how to return the value found  
> > back to the input.
> 
> It would be very helpful if you were to post your code and ask specific  
> questions about problems you are having with your code.

onblur() needs to invoke a javascript function. That function should:  
do an HttpXmlRequest call to the server, see if it gets a usable  
response, and set the input field’s ‘value’ property to the returned  
value if good.

Give your input elements unique id attributes. Makes it easier to  
locate them. Then (assuming the use of prototype.js) you can do  
$(theInputID).value = theReturnedGoodValue in the callback method.

Hand-coding the javascript for this is pretty straightforward, but if  
you get stuck, ask for help on a JavaScript list. It’s off-topic for  
the main Ruby list.

If you are looking to use Rails helper methods or RJS, you would do  
better to ask for help on the Rails list.

–  
James B.

“Programs must be written for people to read, and only incidentally  
for machines to execute.”

- H. Abelson and G. Sussman  
(in "The Structure and Interpretation of Computer Programs)
