# How to Get value of text\_field?

**URL:** <https://www.ruby-forum.com/t/how-to-get-value-of-text-field/162142>\
**Category:** Rails\
**Created:** [March 6, 2009, 6:55am UTC](https://www.ruby-forum.com/t/how-to-get-value-of-text-field/162142 "2009-03-06T06:55:22Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![zeba-momin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/8e7dd6/32.png) [@zeba-momin](https://www.ruby-forum.com/u/zeba-momin)\
**Post date:** [March 6, 2009, 6:55am UTC](https://www.ruby-forum.com/t/how-to-get-value-of-text-field/162142/1 "2009-03-06T06:55:22Z")

</div>

Hi,  
I have a text field as:  
\<%= text\_field :contact ,:name,:class=\>‘roundRect’,:id=\>‘name’ %\>

Then on click of a link I want to pass the value in this text field to a  
controller method.  
I do not want to use a form and form submit.

I have the link as:  
[GO](https://www.ruby-forum.com/main/search)  
and to this ‘search’ method I want to pass the text field value…

How do I do this???  
Thank you…

---

<div class="post-metadata">

**Author:** ![zeba-momin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/8e7dd6/32.png) [@zeba-momin](https://www.ruby-forum.com/u/zeba-momin)\
**Post date:** [March 6, 2009, 4:33pm UTC](https://www.ruby-forum.com/t/how-to-get-value-of-text-field/162142/2 "2009-03-06T16:33:41Z")

</div>

Hello, you may add javascript to onclick event of your link.

[GO](https://www.ruby-forum.com/main/search)

and in js file :

function addParam(link){  
var name = $(‘name’);  
if(name && !name.value.blank()){  
link.href += ‘’?query=" + escpe(name.value);  
return true;  
} else {  
alert(‘Input string for search’);  
return false;  
}  
}

then in your controller use params[:query]

---

<div class="post-metadata">

**Author:** ![zeba-momin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/8e7dd6/32.png) [@zeba-momin](https://www.ruby-forum.com/u/zeba-momin)\
**Post date:** [March 6, 2009, 5:36pm UTC](https://www.ruby-forum.com/t/how-to-get-value-of-text-field/162142/3 "2009-03-06T17:36:43Z")

</div>

Hi Jeba,

On Fri, 2009-03-06 at 06:55 +0100, Jeba M. wrote:

> and to this ‘search’ method I want to pass the text field value…
> 
> How do I do this???  
> Thank you…

The link\_to\_remote helper will do what you want. First, you need to  
wrap the field(s) you want to pass to the controller in a

 with an  
id.
 \<%= text\_field :contact ,:name,:class=\>'roundRect',:id=\>'name' %\>

Then in your link\_to\_remote …

\<%= link\_to\_remote ‘Click me’, :url =\> (:controller =\> ‘main’, :action  
=\> 'search}, :submit =\> ‘field\_to\_submit’, :method =\> :post %\>

When the link is clicked, the value you’re looking for will be passed to  
the controller via a POST and can be accessed using  
params[:contact][:name]

HTH,  
Bill

---

<div class="post-metadata">

**Author:** ![zeba-momin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/8e7dd6/32.png) [@zeba-momin](https://www.ruby-forum.com/u/zeba-momin)\
**Post date:** [March 9, 2009, 7:28am UTC](https://www.ruby-forum.com/t/how-to-get-value-of-text-field/162142/4 "2009-03-09T07:28:57Z")

</div>

Rakoth wrote:

> Hello, you may add javascript to onclick event of your link.
> 
> [GO](https://www.ruby-forum.com/main/search)

Hi  
Thank for helping.  
I tried this…but the problem I’m facing is that,the first time I click  
the link is as:  
[http://localhost:3000/main/search](http://localhost:3000/main/search)  
On the second click the link is as:  
[http://localhost:3000/main/search?query=](http://localhost:3000/main/search?query=)\<text\_field\_value\>  
On the thirdclick the link is as:  
[http://localhost:3000/main/search?query=](http://localhost:3000/main/search?query=)\<text\_field\_value\>?query=\<text\_field\_value\>  
and so on…  
Can u please suggest how do I solve this???  
I’m not well versed with javascripts…  
Thank you again…

bill walton wrote:

> \<%= link\_to\_remote ‘Click me’, :url =\> (:controller =\> ‘main’, :action  
> =\> 'search}, :submit =\> ‘field\_to\_submit’, :method =\> :post %\>
> 
> When the link is clicked, the value you’re looking for will be passed to  
> the controller via a POST and can be accessed using  
> params[:contact][:name]

Thank you for your help…  
But it didn’t work for me… ☹  
My code looks as:

 \<%= text\_field :contact ,:name,:class=\>'roundRect',:id=\>'name' %\>
\<%=link\_to\_remote('Go', :url =\>{:controller =\> 'main', :action=\> 'search'},:submit =\> 'srchname',:method =\>'post')%\>

But it doesn’t even go to the main/search method…  
Where am I going wrong???  
Thank You.

---

<div class="post-metadata">

**Author:** ![zeba-momin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/8e7dd6/32.png) [@zeba-momin](https://www.ruby-forum.com/u/zeba-momin)\
**Post date:** [March 9, 2009, 3:03pm UTC](https://www.ruby-forum.com/t/how-to-get-value-of-text-field/162142/5 "2009-03-09T15:03:17Z")

</div>

Hi Felix,  
On Mon, 2009-03-09 at 05:34 -0700, Felix wrote:

> @bill walton: Can you use the :submit parameter of link\_to\_remote even  
> without wrapping the inputs in form tag? The documentation seems to  
> suggest so, but i’ve never tried …

So give it a try! [straight answer =\> yes 😉]

---

<div class="post-metadata">

**Author:** ![zeba-momin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/8e7dd6/32.png) [@zeba-momin](https://www.ruby-forum.com/u/zeba-momin)\
**Post date:** [March 9, 2009, 1:36pm UTC](https://www.ruby-forum.com/t/how-to-get-value-of-text-field/162142/6 "2009-03-09T13:36:03Z")

</div>

I can’t really say what went wrong with the previous code you tried,  
but here is how i would do it:

\<%= text\_field :contact ,:name,:class=\>‘roundRect’,:id=\>‘name’ %\>  
\<%=link\_to\_remote(‘Go’,  
:url =\>{:controller =\> ‘main’, :action=\> ‘search’},  
:with =\> “‘contact[name]=’ + $(‘contact\_name’).value”)%\>

@bill walton: Can you use the :submit parameter of link\_to\_remote even  
without wrapping the inputs in form tag? The documentation seems to  
suggest so, but i’ve never tried …

On Mar 9, 7:28 am, Jeba M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
