# Newbie Ajaxy Question

**URL:** <https://www.ruby-forum.com/t/newbie-ajaxy-question/58561>\
**Category:** Rails\
**Created:** [April 23, 2006, 7:37am UTC](https://www.ruby-forum.com/t/newbie-ajaxy-question/58561 "2006-04-23T07:37:23Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Terry\_D](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/cc9497/32.png) [@Terry\_D](https://www.ruby-forum.com/u/Terry_D)\
**Post date:** [April 23, 2006, 7:37am UTC](https://www.ruby-forum.com/t/newbie-ajaxy-question/58561/1 "2006-04-23T07:37:23Z")

</div>

Hi all,

I’ve been trying to figure this out by myself and while I feel I’ve made  
some progress, I feel kind of stuck. Let me see if I can explain what  
I’m  
trying to do. I have a very simple form with a text box and a text  
area.  
The text in each of these will eventually make it into a row in a table  
in  
my database. For now, the value in the text box represents a  
del.icio.ustag. Currently, I figured out how to get an ajax observer  
wired up so that  
when the user enters a value into the text box and tabs away, my  
controller  
gets called along with the contents of the text box and the controller  
then  
makes a call to the delicious REST API and strips out bits of the return  
and  
renders them into the text area. This is all presently working, but I’m  
still having some frustrations. I think I’m still missing a big chunk  
of  
understanding. Here’s what I’d like to do:

I’d like to just have a link\_to\_remote to call the action when the user  
clicks a link, but I can’t for the life of me figure out how to read the  
value out of the text box in that case without submitting the form.  
Currently, the text box is created like so: \<%= text\_field\_tag :tags %\>

Originally, this was to be \<%= text\_field ‘query’, ‘tags’ %\> so that the  
value could be passed up to my Create method in the controller. I can  
only  
get my observer to work with the previous rhtml code.

Here’s the bulk of my code:

from the \_form.rhtml partial:

Tags  
\<%= text\_field\_tag :tags %\>

Results  
\<%= text\_area 'query', 'results' %\>

from the new.rhtml:

\<%= form\_remote\_tag :action =\> ‘create’ %\>  
\<%= render :partial =\> ‘form’ %\>  
\<%= submit\_tag “Create” %\>  
\<%= end\_form\_tag %\>

\<%= observe\_field(:tags,  
:update =\> ‘query\_results’,  
:url =\> { :action =\> :get\_results }) %\>

Pertinent bits of the controller:

def get\_results  
render :update do |page|  
@tags = request.raw\_post  
@results = “No Results”

```
 ...
CALL TO DELICIOUS using @tags
...
page['query_results'].value = @results

```

end

Can anyone spare a clue on how I can to a link\_to\_remote and pass the  
value  
of the text box and keep the textbox linked to the query object?

I realize the form\_remote\_tag is probably not necessary with the way I’m  
doing things. I just noticed it - it’s an artifact of earlier, failed  
attempts.

I’m still pretty darned new to both web development and RoR, so please  
forgive me if I’ve just asked a bunch of stupid questions. I’m trying  
to  
learn. 🙂

I have the Agile Pragmatic book and if you can refer me to a section in  
there, that would be great as would pointers to places to look in the  
online  
docs.

Thanks!!

---

<div class="post-metadata">

**Author:** ![Terry\_D](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/cc9497/32.png) [@Terry\_D](https://www.ruby-forum.com/u/Terry_D)\
**Post date:** [April 23, 2006, 9:04am UTC](https://www.ruby-forum.com/t/newbie-ajaxy-question/58561/2 "2006-04-23T09:04:31Z")

</div>

Terry D. wrote:

> I’d like to just have a link\_to\_remote to call the action when the user  
> clicks a link, but I can’t for the life of me figure out how to read the  
> value out of the text box in that case without submitting the form.  
> Currently, the text box is created like so: \<%= text\_field\_tag :tags %\>

This may or may not be the kind of thing you’re looking for, but I had a  
similar situation and here’s how I handled it.

I have a form with search criteria, radio buttons and text fields for  
various fields in my model. It has a standard submit button at the  
bottom. I also have a an observe\_form field which responds every time  
the form is changed, and updates a list at the top of the form which  
says “There are \_\_ number of matches. Show me now.” And the “Show me  
now” part is a link, which also submits the form.

What happens is that when my form observer notices the form has changed,  
it calls a controller action which, among other things, loads a partial  
with the following code in it:

\<%= link\_to “Show me now.”, @url\_params %\>

My controller produces @url\_params by manipulating the params hash sent  
back by the form observer when it calls the “find” action. This way the  
parameters in the URL are dynamically generated based on the form  
fields.

It also suits my purposes by using a GET request so that an individual  
search request can be bookmarked or viewed again with the “back” button.

Jeff C.man

---

<div class="post-metadata">

**Author:** ![Terry\_D](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/cc9497/32.png) [@Terry\_D](https://www.ruby-forum.com/u/Terry_D)\
**Post date:** [April 23, 2006, 10:19am UTC](https://www.ruby-forum.com/t/newbie-ajaxy-question/58561/3 "2006-04-23T10:19:37Z")

</div>

Terry D. wrote:

> called along with the contents of the text box and the controller then

Funny, I was struggling with this very same problem yesterday with an  
Amazon search – think after a couple of hours of searching and reading  
Prototype docs I’ve managed to get it sorted (though am a real AJAX  
noob, so stand to be corrected).

My text field looks like this:

```
<%= text_field 'resource', 'title', 'size' => 50, 'id' => 

```

“titlediv1” %\>

And the link\_to\_remote looks like this:

\<%= link\_to\_remote “Search Amazon “, :update =\> “amazondiv”,  
:with=\>”‘term=’ + $F(‘titlediv1’)”,  
:url =\> { :action =\> “searchabytitle”, :id =\> @modtype } %\>

:with (I think) allows you to supply a javascript expression (it’s sorta  
documented in the observe\_field rdoc, but I believe it works on other  
functions also) to supply parameters

$F() is a shorthand way of writing Form.Element.getValue(), so  
$F(‘titlediv1’) gets the value of what’s in the form field with the id  
of titlediv1.

As I said, no guarantees, but it seems to be working and is passing  
without any errors (BTW definitely advise getting the Firebug extension  
for Firefox, and selecting ShowXMLHttpREquests in the console options –  
makes it so much easier to see what’s being submitted (and returned).

Hope this helps

---

<div class="post-metadata">

**Author:** ![Terry\_D](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/cc9497/32.png) [@Terry\_D](https://www.ruby-forum.com/u/Terry_D)\
**Post date:** [April 23, 2006, 10:07pm UTC](https://www.ruby-forum.com/t/newbie-ajaxy-question/58561/4 "2006-04-23T22:07:57Z")

</div>

Is passing the $F(‘query\_tags’) kind of a hack? Or is it the  
established  
way to do this?

Terry

---

<div class="post-metadata">

**Author:** ![Terry\_D](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/cc9497/32.png) [@Terry\_D](https://www.ruby-forum.com/u/Terry_D)\
**Post date:** [April 23, 2006, 5:03pm UTC](https://www.ruby-forum.com/t/newbie-ajaxy-question/58561/5 "2006-04-23T17:03:13Z")

</div>

Thanks guys! These are interesting ways to get at that value, but it  
seems  
so convoluted to get just a simple little textbox value. One thing that  
I’ve found really frustrating is that the documentation is so sparse. I  
love how the documentation says, send this parameter, this parameter and  
then a hash of options, but don’t bother telling you what the possible  
options are. On the page with link\_to\_remote, there are some options  
listed  
up at the top, but I can’t find any place that describe exactly what  
each  
one is for. I’m guessing this is just a symptom of technology that’s so  
new.

If there’s any Ajax/RoR experts out there, can you please shine some  
more  
light on this issue? It seems there’s other people interested as well.

Thanks again!

Terry

---

<div class="post-metadata">

**Author:** ![Terry\_D](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/cc9497/32.png) [@Terry\_D](https://www.ruby-forum.com/u/Terry_D)\
**Post date:** [September 27, 2006, 8:19pm UTC](https://www.ruby-forum.com/t/newbie-ajaxy-question/58561/6 "2006-09-27T20:19:51Z")

</div>

AAAAHHHH!!! Thanks Chris…been beating my head against the wall for a  
day or two now to handle this issue. It works!

I did find one interesting “feature” others may run into – because I  
did. Whitespace counts! It concerns this line:

:with=\>"‘term=’ + $F(‘titlediv1’)",

if you happen to write it like this:

:with=\>"'term = ’ + $F(‘titlediv1’)",

you’ll get a parameters array with “term " as a key and " \<titlediv1’s  
value\>”, i.e. an extra space after the key and before the value. That  
threw me for a loop until I look hard enough at params.inspect results.

- David

Chris T wrote:

> Terry D. wrote:
> 
> > called along with the contents of the text box and the controller then
> 
> Funny, I was struggling with this very same problem yesterday with an  
> Amazon search – think after a couple of hours of searching and reading  
> Prototype docs I’ve managed to get it sorted (though am a real AJAX  
> noob, so stand to be corrected).
> 
> My text field looks like this:
> 
> ```
> <%= text_field 'resource', 'title', 'size' => 50, 'id' => 
> 
> ```
> 
> “titlediv1” %\>
> 
> And the link\_to\_remote looks like this:
> 
> \<%= link\_to\_remote “Search Amazon “, :update =\> “amazondiv”,  
> :with=\>”‘term=’ + $F(‘titlediv1’)”,  
> :url =\> { :action =\> “searchabytitle”, :id =\> @modtype } %\>
> 
> :with (I think) allows you to supply a javascript expression (it’s sorta  
> documented in the observe\_field rdoc, but I believe it works on other  
> functions also) to supply parameters
> 
> $F() is a shorthand way of writing Form.Element.getValue(), so  
> $F(‘titlediv1’) gets the value of what’s in the form field with the id  
> of titlediv1.
> 
> As I said, no guarantees, but it seems to be working and is passing  
> without any errors (BTW definitely advise getting the Firebug extension  
> for Firefox, and selecting ShowXMLHttpREquests in the console options –  
> makes it so much easier to see what’s being submitted (and returned).
> 
> Hope this helps
