Forum: Ruby on Rails Ajax Help: Multiple Updates - ?

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.
C9043a4e457c953ff465f497b39f345e?d=identicon&s=25 Randy Marmer (rmarmer1)
on 2005-12-24 04:55
Hi!

Wondering if anyone can tell me what's wrong with my code?

I'm trying to use the onChange event of a select list to update multiple
divs...

Agile Web Development with Rails (p. 403) suggests using ":complete =>
eval(request.responseText) instead of :update"

I've attempted the following test code:

# in the view

<select name="plist", class="required", 				onchange="<%=
remote_function(:complete => evaluate_remote_response, :url => { :action
=> :updateDivs }) %>" >

<option>Select a Person</option>
<option>Select a Dog</option>
<option>Select a Frog</option>
</select>

<br>

<div id="detail1"> </div>

<br>

<div id="detail2"> </div>

<br>

<div id="detail3"> </div>

<br>

<div id="detail4"> </div>


# controller

    def updateDivs
        render(:layout => false)
    end


# updateDivs.rhtml

<%= javascript_tag(

update_element_function(
        "detail1", :position => :top, :content =>  "testData1" ) +

update_element_function(
        "detail2", :position => :top, :content =>  "testData2" ) +

update_element_function(
        "detail3", :position => :top, :content =>  "testData3" ) +

update_element_function(
        "detail4", :position => :top, :content =>  "testData4" )) %>


I don't get any errors, but the testdata is not inserted into the divs
either.


The example in the book replaces the :update with a :complete, but this
is done on a "link_to_remote()" not on a "remote_function()" as I have
done... I assumed that this would be OK because the documentation says
that remote_function "Takes the same arguments as link_to_remote"?!?

Just for grins I tried adding the following:

     <%= link_to_remote "Try Me",
      :complete => "eval(request.responseText)",
      :url => { :action => :updateDivs } %>

Again, no errors, but no output!

Both the link & the select onchange fire the updateDivs method & produce
exactly the same log output for both:


link/"Try Me" log output:
Processing TestemController#updateDivs (for 127.0.0.1 at 2005-12-23
22:41:17) [POST]
  Parameters: {"action"=>"updateDivs", "controller"=>"testem"}
Rendering testem/updateDivs
Completed in 0.01366 (73 reqs/sec) | Rendering: 0.00871 (63%) | 200 OK
[http://localhost/testem/updateDivs]


Select/onChange log output:
Processing TestemController#updateDivs (for 127.0.0.1 at 2005-12-23
22:41:23) [POST]
  Parameters: {"action"=>"updateDivs", "controller"=>"testem"}
Rendering testem/updateDivs
Completed in 0.00775 (129 reqs/sec) | Rendering: 0.00353 (45%) | 200 OK
[http://localhost/testem/updateDivs]

Any help would be appreciated, I'm really stuck!

Thanks!
C64e63b70be7dfed8b0742540b8b27e5?d=identicon&s=25 Mark Reginald James (Guest)
on 2005-12-24 20:18
(Received via mailing list)
randy marmer wrote:

> update_element_function(
>         "detail3", :position => :top, :content =>  "testData3" ) +
>
> update_element_function(
>         "detail4", :position => :top, :content =>  "testData4" )) %>
>
>
> I don't get any errors, but the testdata is not inserted into the divs
> either.

Get rid of the "javascript_tag" wrapper.  The remote response is being
evaluated, so you don't want to rap it in <script> tags.


--
We develop, watch us RoR, in numbers too big to ignore.
C9043a4e457c953ff465f497b39f345e?d=identicon&s=25 randy marmer (Guest)
on 2005-12-24 23:11
Mark Reginald James wrote:
> randy marmer wrote:
>
>> update_element_function(
>>         "detail3", :position => :top, :content =>  "testData3" ) +
>>
>> update_element_function(
>>         "detail4", :position => :top, :content =>  "testData4" )) %>
>>
>>
>> I don't get any errors, but the testdata is not inserted into the divs
>> either.
>
> Get rid of the "javascript_tag" wrapper.  The remote response is being
> evaluated, so you don't want to rap it in <script> tags.

INDEED! Thanks!
This topic is locked and can not be replied to.