Update div using render:update

Hi! I am trying to show devices in my database in a table format and in each column corresponding to one device im trying to give a ping link which will ping the device and update the result of ping just below the ping link.

What problem im facing is every time first cell is updated for ping result of any device. this is clearly a problem Related to div id's , how to distinguish each cell's div id's.

Kindly have a look at my code : My view looks like:

<table id="myTable" width="800" border='1' >   <thead>     <tr>       <th width="130" scope="col">Device Name</th>        <th width="70" scope="col">Status</th>          </tr>   </thead>   <tbody>        <% @devices.each do |c|%>      <tr>        <td>           <%=h c.name%>                   </td>             <td> <%= javascript_include_tag:defaults%>           <%= link_to_remote "Ping",:url => {:action => 'show_ping_ip_addr',:ipaddr_id=>Ipaddr.find(:first,:conditions =>["dev_id=?",c.id])} %><br> <div id="ping_result" style="display:none; ">         </div> </td>       </tr> </table>

My controller looks like: def show_ping_ip_addr              @ipadd=Ipaddr.find_by_id(params[:ipaddr_id])            if system("ping #{@ipadd.address}")         res="Yes it is connected"       else         res="Host unreachable"       end       logger.error "variable is equal to: #{res}"             render :update do |page|       #logger.error "Got here 2"       page.replace_html 'ping_result', (res || "No Results").to_s       page.show 'ping_result'       page.visual_effect:highlight, 'ping_result', :duration => 0.2            end        end

Thanx in advance!!

What problem im facing is every time first cell is updated for ping result of any device. this is clearly a problem Related to div id's , how to distinguish each cell's div id's.

The whole point of ids is that they are unique, if they aren't results are undefined. Just make them unique (eg append the id of the Ipaddr record). There are even helpers like dom_id, div_for etc...

Fred

I tried to make the id’s unique through the following way:

view now looks like:

<%= javascript_include_tag:defaults%> <%= link_to_remote "Ping",:update=>"ping_result_#{[c.id](http://c.id)}",:url => {:action => 'show_ping_ip_addr1',:ipaddr_id=>Ipaddr.find(:first,:conditions =>["dev_id=?",[c.id](http://c.id)])} %>

and controller: def show_ping_ip_addr1

  @ipadd=Ipaddr.find_by_id(params[:ipaddr_id])

  devid=@ipadd.dev_id
  if system("ping  #{@ipadd.address}")
    res="Yes it is connected"
  else
    res="Host unreachable"
  end
  logger.error "variable is equal to: #{res}"

  render_text "ping"+ res.to_s

end

but its not displaying anything!!

Hi

i tried another way to do it… this time its giving RJS error(Object Error)

view now looks like:

<%= javascript_include_tag:defaults%> <%= link_to_remote "Ping",:url => {:action => 'show_ping_ip_addr',:ipaddr_id=>Ipaddr.find(:first,:conditions =>["dev_id=?",[c.id](http://c.id)])} %>
and controller:

def show_ping_ip_addr

  @ipadd=Ipaddr.find_by_id(params[:ipaddr_id])

  devid=@ipadd.dev_id
  if system("ping  #{@ipadd.address}")
    res="Yes it is connected"
  else
    res="Host unreachable"
  end
 
 
  render :update do |page|

  page.replace_html 'ping_result_'+devid.to_s, (res || "No Results").to_s
  page.show 'ping_result_'+devid.to_s
  page.visual_effect:highlight, 'ping_result_'+devid.to_s, :duration => 0.2

end

end

it is correctly identifying unique ids for divs this time in both controller and view(i checked by putting logger statements around) but i cudnt understand the cause for error.

Please someone have a look at it… and help me out!! Thanx alot!!!

Hi Supriya,

supriya agarwal wrote:

i tried another way to do it... this time its giving RJS error(Object Error)

it is correctly identifying unique ids for divs this time in both controller and view

Perhaps in the controller, but in looking at your view code, I can't help but wonder about the rendered page. I'd start by taking the rendered page source (before you try to do any RJS updates on it) and run it through the W3C validator. That'll tell you real quick if you've got unique id's or not.

HTH, Bill

Hey thanx Bill for replying… I am not sure about your idea… can you just elaborate it giving an example.

Supriya

Hey i am able to do it… small error in my previous code:

      <%= link_to_remote ("Ping",:update=>"ping_result_#{[c.id](http://c.id)}",:url => {:action => 'show_ping_ip_addr1',:ipaddr_id=>Ipaddr.find(:first,:conditions =>["dev_id=?",[c.id](http://c.id)])}) %><br>

Thanx!