How to change the text in a text_field_tag

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 | %>
    <p>
  <% form_remote_tag :url => {:action => :add_to_speech_box, :speech =>
bit_of_text } do %>
    <%= submit_tag bit_of_text %>
  <% end %>
    </p>

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

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.

   <p>
<% form_remote_tag :url => {:action => :add_to_speech_box, :speech =>
bit_of_text } do %>
<%= submit_tag bit_of_text %>
<% end %>
   </p>

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

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

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

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 <div id="whatever_you_want_to_call_it"> ... </div>, 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

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
  <div id='speech_text_field'>
    <%= render(:partial => "speech_text_entry") %>
  </div>
<% 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

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.

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

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

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

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.