How to Get value of text_field ??

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: <a href ="/main/search" >GO</a> and to this 'search' method I want to pass the text field value....

How do I do this??? Thank you...

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

<a href ="/main/search" onclick="return addParam(this);">GO</a>

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]

Hi Jeba,

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: <a href ="/main/search" >GO</a> 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 <div> with an id.

<div id='field_to_submit'>   <%= text_field :contact ,:name,:class=>'roundRect',:id=>'name' %> </div>

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

Rakoth wrote:

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

<a href ="/main/search" onclick="return addParam(this);">GO</a>

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 On the second click the link is as: http://localhost:3000/main/search?query=&lt;text\_field\_value&gt; On the thirdclick the link is as: http://localhost:3000/main/search?query=&lt;text\_field\_value&gt;?query=&lt;text\_field\_value&gt; 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.... :frowning: My code looks as: <div id='srchname'>   <%= text_field :contact ,:name,:class=>'roundRect',:id=>'name' %> </div> <%=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.

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

Hi Felix,