Forum: Ruby on Rails How to Get value of text_field ??

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
D8f444b5c1b0304a2b98d5b16da0f2f1?d=identicon&s=25 Jeba Momin (zeba)
on 2009-03-06 06:55
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...
C4eb02ef0d5e1474bbfe5be14ed6adf8?d=identicon&s=25 Rakoth (Guest)
on 2009-03-06 16:33
(Received via mailing list)
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]
91a72b0ad3e43e3ef3a31816667ec694?d=identicon&s=25 bill walton (Guest)
on 2009-03-06 17:36
(Received via mailing list)
Hi Jeba,

On Fri, 2009-03-06 at 06:55 +0100, Jeba Momin 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 <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
D8f444b5c1b0304a2b98d5b16da0f2f1?d=identicon&s=25 Jeba Momin (zeba)
on 2009-03-09 07:28
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=<text_fiel...
On the thirdclick the link is as:
http://localhost:3000/main/search?query=<text_fiel...
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:
<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.
Ea52242ae25d195415cd9ef31db72aeb?d=identicon&s=25 Felix (Guest)
on 2009-03-09 13:36
(Received via mailing list)
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 Momin <rails-mailing-l...@andreas-s.net>
91a72b0ad3e43e3ef3a31816667ec694?d=identicon&s=25 bill walton (Guest)
on 2009-03-09 15:03
(Received via mailing list)
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 ;-) ]
This topic is locked and can not be replied to.