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