# How to change the text in a text\_field\_tag

**URL:** <https://www.ruby-forum.com/t/how-to-change-the-text-in-a-text-field-tag/92146>\
**Category:** Rails\
**Created:** [April 16, 2007, 4:52pm UTC](https://www.ruby-forum.com/t/how-to-change-the-text-in-a-text-field-tag/92146 "2007-04-16T16:52:57Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![jay](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/ed8c4c/32.png) [@jay](https://www.ruby-forum.com/u/jay)\
**Post date:** [April 16, 2007, 4:52pm UTC](https://www.ruby-forum.com/t/how-to-change-the-text-in-a-text-field-tag/92146/1 "2007-04-16T16:52:57Z")

</div>

This is my first post here. Hope someone can help.

I have what I think is a simple problem but I can’t find a solution.

I have these two forms:

\<% @general\_canned\_text.each do | bit\_of\_text | %\>

\<% form\_remote\_tag :url =\> {:action =\> :add\_to\_speech\_box, :speech =\>  
bit\_of\_text } do %\>  
\<%= submit\_tag bit\_of\_text %\>  
\<% end %\>

\<% end %\>

\<% form\_remote\_tag :id =\> “speech\_field”, :url =\> { :action  
=\> :add\_to\_speech\_queue } do %\>  
Speak  
\<%= text\_field\_tag( “speech”, @text, :size =\> “20”, :maxlength =\>  
“40”) %\>  
\<% end %\>

The bottom one is just a text box. The top form contains several  
buttons with text associated.

When a button on the top form is changed I’d like the text in the  
bottom form text field to change. I can get it to change some div on  
the page but not the bottom form’s text field.

Does anybody know how to do this? Sorry if I missed something obvious.

Cheers,  
Jay

---

<div class="post-metadata">

**Author:** ![jay](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/ed8c4c/32.png) [@jay](https://www.ruby-forum.com/u/jay)\
**Post date:** [April 16, 2007, 5:12pm UTC](https://www.ruby-forum.com/t/how-to-change-the-text-in-a-text-field-tag/92146/2 "2007-04-16T17:12:59Z")

</div>

Hi Jay,

jay wrote:

> I have these two forms:
> 
> \<% @general\_canned\_text.each do | bit\_of\_text | %\>

Not your question, but the line above implies you’re going to have more  
than  
two forms.

> =\> :add\_to\_speech\_queue } do %\>  
> the page but not the bottom form’s text field.  
> When some element in it is changed? Or when the form is submitted? If  
> ‘element changed’ then you can use observe\_field to make an Ajax request  
> to  
> the controller and then use RJS to update any DOM element on the page.  
> If  
> ‘form submitted’ then the you might be better off rendering the second  
> form  
> as a partial, and re-rendering it if needed when the form is submitted.

hth,  
Bill

---

<div class="post-metadata">

**Author:** ![jay](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/ed8c4c/32.png) [@jay](https://www.ruby-forum.com/u/jay)\
**Post date:** [April 16, 2007, 5:29pm UTC](https://www.ruby-forum.com/t/how-to-change-the-text-in-a-text-field-tag/92146/3 "2007-04-16T17:29:02Z")

</div>

On Apr 16, 4:11 pm, “Bill W.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > When a button on the top form is changed I’d like the text in the  
> > Bill  
> > Thanks for your reply. I do indeed have many forms in the loop which  
> > is as I wanted it. They just consist of buttons so I want the bottom  
> > form to show different text when any of the first set of forms is  
> > submitted. So it is your second suggestion - ‘form submitted’.

But it is in the action the form calls that I don’t know how to get  
hold of the text field in the bottom form to change it. Can I just do  
something like name\_of\_text\_filed.value = “blah” or is there another  
way of updating the form?

I’ll have a look at rendering partials for now.

Thank you.  
Jay

---

<div class="post-metadata">

**Author:** ![jay](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/ed8c4c/32.png) [@jay](https://www.ruby-forum.com/u/jay)\
**Post date:** [April 16, 2007, 6:35pm UTC](https://www.ruby-forum.com/t/how-to-change-the-text-in-a-text-field-tag/92146/4 "2007-04-16T18:35:10Z")

</div>

Hi Jay,

jay wrote:

> Thanks for your reply.

You’re welcome.

> I do indeed have many forms in the loop which is as I wanted it.  
> They just consist of buttons so I want the bottom form to show  
> different text when any of the first set of forms is submitted.  
> So it is your second suggestion - ‘form submitted’.
> 
> But it is in the action the form calls that I don’t know how to get  
> hold of the text field in the bottom form to change it. Can I just do  
> something like name\_of\_text\_filed.value = “blah” or is there another  
> way of updating the form?

You’ll need to wrap your text\_field\_tag in a … , create a partial (you can  
just leave the form as is for the initial page render) with that same  
content, and then use RJS page.replace\_html to render the partial when  
the  
visitor clicks one of your buttons. Whatever processing you’re doing in  
your method should create the @text instance variable you’ll need to  
populate the field when you render your partial. If you’re just  
updating  
that one field, it’s acceptable form to do it in the controller method  
rather than creating a separate RJS template.

hth,  
Bill

---

<div class="post-metadata">

**Author:** ![jay](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/ed8c4c/32.png) [@jay](https://www.ruby-forum.com/u/jay)\
**Post date:** [April 16, 2007, 10:59pm UTC](https://www.ruby-forum.com/t/how-to-change-the-text-in-a-text-field-tag/92146/5 "2007-04-16T22:59:42Z")

</div>

Hi Jay,

jay wrote:

> Thank you again for your help. That worked great and the  
> text in the field is now being updated.

Congrats!

> I’ve now come up against another problem which I think is  
> a simple misunderstanding on my part.

> The problem is that the text is being replaced entirely rather than  
> being concatenated as I intended. I’m stumped. It seems like I’ve set  
> it up okay but when I look at the speech\_entry variable

I assume, by ‘the speech\_entry variable’, you actually mean  
@speech\_text.  
Yes?

> it’s empty before the append function.

Absolutely. As it should be. A Rails app lives for exactly one request  
/  
response cycle. The only thing that gives it a life that’s _apparently_  
longer than that is data stored in the database or in session variables.  
If  
you want @speech\_text to have content prior to the append, you’re going  
to  
have to get that content from one of those two places.

hth,  
Bill

---

<div class="post-metadata">

**Author:** ![jay](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/ed8c4c/32.png) [@jay](https://www.ruby-forum.com/u/jay)\
**Post date:** [April 17, 2007, 11:03am UTC](https://www.ruby-forum.com/t/how-to-change-the-text-in-a-text-field-tag/92146/6 "2007-04-17T11:03:45Z")

</div>

On Apr 16, 9:58 pm, “Bill W.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > def add\_to\_speech\_box  
> > being concatenated as I intended. I’m stumped. It seems like I’ve set  
> > you want @speech\_text to have content prior to the append, you’re going to  
> > have to get that content from one of those two places.
> 
> hth,  
> Bill

Than you again for your advice. It all seems pretty obvious when it’s  
pointed out. I knew all these things in theory but putting them into  
practice is another thing. I’m looking into sessions in my rails book  
right now. I was wondering if there’s a way to include the value of  
the bottom form in the call made by one of the top forms. That would  
be very simple and would mean I didn’t need to use sessions.

But that may not be possible so I’ll look at sessions.

Thank you for your advice.  
Jay

---

<div class="post-metadata">

**Author:** ![jay](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/ed8c4c/32.png) [@jay](https://www.ruby-forum.com/u/jay)\
**Post date:** [April 22, 2007, 11:20pm UTC](https://www.ruby-forum.com/t/how-to-change-the-text-in-a-text-field-tag/92146/7 "2007-04-22T23:20:49Z")

</div>

For anybody interested. I tried rendering partials and they’re not as  
difficult as I thought. I’m on to the next problem now. Thanks for  
your help Bill.

---

<div class="post-metadata">

**Author:** ![jay](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/ed8c4c/32.png) [@jay](https://www.ruby-forum.com/u/jay)\
**Post date:** [April 16, 2007, 10:46pm UTC](https://www.ruby-forum.com/t/how-to-change-the-text-in-a-text-field-tag/92146/8 "2007-04-16T22:46:38Z")

</div>

On Apr 16, 5:34 pm, “Bill W.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > different text when any of the first set of forms is submitted.  
> > content, and then use RJS page.replace\_html to render the partial when the  
> > visitor clicks one of your buttons. Whatever processing you’re doing in  
> > your method should create the @text instance variable you’ll need to  
> > populate the field when you render your partial. If you’re just updating  
> > that one field, it’s acceptable form to do it in the controller method  
> > rather than creating a separate RJS template.
> 
> hth,  
> Bill

Bill,

Thank you again for your help. That worked great and the text in the  
field is now being updated. I’ve now come up against another problem  
which I think is a simple misunderstanding on my part.

The index.rhtml part is

\<% form\_remote\_tag :url =\> { :action =\> :add\_to\_speech\_queue } do %\>  
Speak

 \<%= render(:partial =\> "speech\_text\_entry") %\> 
\<% end %\>

The partial is very simply:

\<%= text\_field\_tag( “speech”, @speech\_text, :size =\> “20”, :maxlength  
=\> “40”) %\>

The controller is:

class WizardController \< ApplicationController

def initialize  
@speech\_text = “”

```
@general_canned_text = Array.new
@general_canned_text.push("Canned text one")
@general_canned_text.push("Canned text two")

```

end

def index

end

def add\_to\_speech\_box  
@speech\_text \<\< params[:speech]  
render :update do | page |  
page.replace\_html ‘speech\_text\_field’, render(:partial =\>  
“speech\_text\_entry”)  
end  
end

end

The problem is that the text is being replaced entirely rather than  
being concatenated as I intended. I’m stumped. It seems like I’ve set  
it up okay but when I look at the speech\_entry variable it’s empty  
before the append function.

Jay
