How to pass current value of form field in link_to?

Greetings to you,

I'm trying to write a "preview" link, and need to pass along the current value of a text_field to this link.

I figure there is some simple Javascript way to do this, but so far haven't figured it out.

Does anyone know how to get and pass the current value of a text_field to link_to?

Where is the value in the text field coming from? If it's coming from your app (e.g. from the database or from a user passing parameters), then you can do this on the Rails side of things, and it seems most likely that you want to set an instance variable in the controller (e.g. @text_field_value = [value]), which you can then use in both your link_to and text_field. If you want the text field to dynamically update as the value of the text field is changed, then you should use Javascript, such as something along these lines:

<input type="text" onkeyup="$('link').innerHTML = this.value" /> <a href="preview..." id="link"> Default text </a>

Best of luck,

Greg

<%= link_to "Preview Post", {:action => :preview, :id => @topic.id, :body => @body}, {:id => 'link', :target => "_blank"} %>

try this:

<%= link_to "Preview Post", {:action => :preview, :id => @topic.id}, {:onclick => "this.href +='?body='+encodeURIComponent($F ('id_of_the_form_element')); return true", :id => 'link', :target => "_blank"} %>

Glad it worked for you. :slight_smile:

John Yerhot wrote:

try this:

<%= link_to "Preview Post", {:action => :preview, :id => @topic.id}, {:onclick => "this.href +='?body='+encodeURIComponent($F ('id_of_the_form_element')); return true", :id => 'link', :target => "_blank"} %>

Hi John, I have a similar requirement. I need to pass the value of a text field with link_to What I'm doing is: <%= link_to "Go", {:controller=>'main',:action=>'searchcontc'},:onclick => "this.href +='?uname='+encodeURIComponent($F('name')); return true" %>

But this doesn't work properly. The first time I click the link it redirects to: http://localhost:3000/main/searchcontc And on the second click it goes to: http://localhost:3000/main/searchcontc?uname=&lt;value\_of\_text\_filed&gt;

Can please help me..and let me know how do I correct this... Thank you.