Newbie Ajaxy Question

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. :slight_smile:

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!!

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

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

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

Terry

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

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