How to Get value of text_field?


#1

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
and to this ‘search’ method I want to pass the text field value…

How do I do this???
Thank you…


#2

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

GO

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]


#3

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


#4

Rakoth wrote:

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

GO

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=<text_field_value>
On the thirdclick the link is as:
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… :frowning:
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.


#5

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 :wink: ]


#6

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