首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >Rails -在循环中呈现Ajax

Rails -在循环中呈现Ajax
EN

Stack Overflow用户
提问于 2018-06-04 00:59:43
回答 1查看 156关注 0票数 0

我想用ajax更新视图中的几个单元格。是否有机会在循环期间多次运行正常运行时间部分?目前,循环遍历所有给定的记录,但部分记录运行一次。

代码语言:javascript
复制
def CheckUptimes
  require 'net/ssh'
  @username = "updater"
  @password = "üqwlp+ß$2"
  @cmd = "uptime"
  @items = Item.all.where("(category = 'Ubuntu 14 LTS')")
  @items.each do |ci|
    @hostname = ci.name
    begin
      ssh = Net::SSH.start(@hostname, @username, :password => @password)
      @uptime = ssh.exec!(@cmd)
      ssh.close
      @uptime = @uptime.strip
      ci.update_attributes(:uptime => @uptime)
      respond_to do |format|
        format.html
        format.js { render :partial => "uptime", :locals => { :id => ci.id, :uptime => @uptime } }
      end
    rescue
      puts "Unable to connect to #{@hostname} using #{@username}/#{@password}"
    end
  end
end
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-06-04 04:03:32

如果我理解得很好,我认为您可以在两个实例变量(数组)中保存哪些主机名能够连接,哪些主机名不能连接,然后在checkuptimes.js.erb中显示哪些主机名可以使用render collection

像这样的东西

代码语言:javascript
复制
@con_ok=@con_ko=[]
@items.each do |ci|
@hostname = ci.name
begin
  ssh = Net::SSH.start(@hostname, @username, :password => @password)
  @uptime = ssh.exec!(@cmd)
  ssh.close
  @uptime = @uptime.strip
  ci.update_attributes(:uptime => @uptime)
  con_ok<<ci.id
rescue
   con_ko<< ci.id
end
respond_to do |format|   ## Not necessary ##
   format.html
   format.js
end

checkuptimes.js.erb

代码语言:javascript
复制
$("#mydiv").html("<%= escape_javascript(render 'uptime', collection: @con_ok)%>");

在此示例中,部分uptime将与items包含@con_ok的次数一样多次呈现,其中包含一个带有数组中的item的局部变量con_ok (id)

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50668838

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档