Accessing form field contents

I have an HTML form on the screen, that is connected to a database
(standard Ruby way). I found that there are people that would rather not
fill in this information, but would rather search Amazon for the info.
Therefore, I added a link to the right of one of the fields that, when
pressed, will search Amazon for what ever you typed into that field.

So, how can I do that? At first I tried adding a line to the link’s
function in my controller, where I get the value of the “book” by doing
@test = params[:book]. Similar to the way that ‘create’ does it; but,
remember, I don’t want to save this book yet, so I don’t do everything
that create does. This always gives me a blank hash value, leading me to
believe that the hash hasn’t been stored yet, because nobody pressed the
“Create” button.

So the next thought is to actually pass in the value when you press the
link, along the lines of:
<%= link_to(“Lookup by title”, :action => ‘lookup_by_title’, :val =>
fieldValue) %>

But how do I get the contents of the field? In JavaScript I’d do
document.formName.fieldName.value, but I can’t do that here because 1.
what is the name of this form? and 2. If I simply say document. blah
blah blah, Ruby barfs because it doesn’t know what document is.

Any thoughts?

On 7/6/06, phillip [email protected] wrote:

that create does. This always gives me a blank hash value, leading me to
what is the name of this form? and 2. If I simply say document. blah
blah blah, Ruby barfs because it doesn’t know what document is.

Any thoughts?

It seems like you’re on the right track, you’re certainly going to be
needing some js/ajax to get this done. Have you looked into RJS at all
or the built-in AJAX functions in Ruby? I have to admit that I’m
pretty woefully inept when it comes to js so I’ll have to leave it
there. link_to_remote might be a great place to start.

If you wanted to do it without ajax you could have the form post, and
if there’s some indicator that they want to search for something on
amazon then you could call render again to re-render the form with the
variables still intact. It would certainly be less appealing than the
js though.

As to the question of what it’s called, you can view the source for
the exact answer but it’s usually got
id=(model_name)_(model_attribute) without the parans and it’s
name=model_name[model_attribute] so you can easily call
document.getElementById(‘book_title’) to get the element and play with
it there.

Cheers,
Chuck V.

Chuck V. wrote:

As to the question of what it’s called, you can view the source for
the exact answer but it’s usually got
id=(model_name)_(model_attribute) without the parans and it’s
name=model_name[model_attribute] so you can easily call
document.getElementById(‘book_title’) to get the element and play with
it there.

Thanks Chuck. I appreciate your help.
Lets say I wanted to do the simple way, and stay away from Ajax/JS at
this point… :slight_smile:
If I wanted to refer to the contents of a field, while still in my view
(and not at the controller level), how would I do that?

For example, lets say that I wanted what ever I type in the field to be
passed into a function, what would I do?

Title: <%= text_field 'book', 'title', "size"=>49 %> <%= link_to("Lookup by title", :action => 'lookup_by_title', :val => 'default') %>

In the above case, I pass ‘default’ as a value called val. What I really
want is the contents of the field ‘title’ (of the model book), to be
passed into the function as in…

Title: <%= text_field 'book', 'title', "size"=>49 %> <%= link_to("Lookup by title", :action => 'lookup_by_title', :val => book.title) %>

Now, unfortunately that doesn’t work. But neither does book[title],
@book.title, @book[title], book_title, etc. etc. Is there any way, in
Ruby, to refer to a form control?

On 7/6/06, phillip [email protected] wrote:

this point… :slight_smile:

@book.title, @book[title], book_title, etc. etc. Is there any way, in
Ruby, to refer to a form control?

Apologies for the delay. Work.

Not without js/ajax or a post/get to the controller. Rails is server
side so it doesn’t know what’s going to be in that field without the
user sending the whole thing back via a post/get or just that part via
js/ajax.

Avoiding js at this point is a little counterproductive, learning RJS
would be well worth it. Regardless, were you to do it you could post
the whole thing back to the controller, save it in a temp table or in
memory, display the search link or whatever, then draw up the old data
later when they finish searching for a book. However, RJS makes AJAX
easy and fun, go learn it and prosper! :slight_smile:

Cheers,
Chuck V.