# Accessing form field contents

**URL:** <https://www.ruby-forum.com/t/accessing-form-field-contents/97372>\
**Category:** Rails\
**Created:** [July 6, 2006, 5:51pm UTC](https://www.ruby-forum.com/t/accessing-form-field-contents/97372 "2006-07-06T17:51:50Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![phillip](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/3e96dc/32.png) [@phillip](https://www.ruby-forum.com/u/phillip)\
**Post date:** [July 6, 2006, 5:51pm UTC](https://www.ruby-forum.com/t/accessing-form-field-contents/97372/1 "2006-07-06T17:51:50Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![phillip](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/3e96dc/32.png) [@phillip](https://www.ruby-forum.com/u/phillip)\
**Post date:** [July 6, 2006, 6:36pm UTC](https://www.ruby-forum.com/t/accessing-form-field-contents/97372/2 "2006-07-06T18:36:41Z")

</div>

On 7/6/06, phillip [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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.

---

<div class="post-metadata">

**Author:** ![phillip](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/3e96dc/32.png) [@phillip](https://www.ruby-forum.com/u/phillip)\
**Post date:** [July 6, 2006, 7:52pm UTC](https://www.ruby-forum.com/t/accessing-form-field-contents/97372/3 "2006-07-06T19:52:22Z")

</div>

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… 🙂  
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?

---

<div class="post-metadata">

**Author:** ![phillip](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/3e96dc/32.png) [@phillip](https://www.ruby-forum.com/u/phillip)\
**Post date:** [July 15, 2006, 1:23am UTC](https://www.ruby-forum.com/t/accessing-form-field-contents/97372/4 "2006-07-15T01:23:50Z")

</div>

On 7/6/06, phillip [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> this point… 🙂
> 
> @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! 🙂

Cheers,  
Chuck V.
