#1 的答案很简单,系统调用,即
ret = system('ls','-l')
如果命令的退出状态为零,则ret 将为真。美元?将包含一个 Process::Status 对象,您可以从中获取退出状态
unless system('ls','-l','/a_bogus_dir')
logger.debug("ls failed with #{$?.exitstatus}")
end
#2 的答案可以通过多种方式完成。您可以创建一个控制器操作,该操作只需抓取文件系统中特定文件的内容并返回内容。
def get_file_contents
File.open(params[:file_to_read],"r") { |f| @contents = f.read }
respond_to |format|
format.js
end
end
然后创建文件get_file_contents.js.erb:
$('#display_div').html('<%= escape_javascript(@contents) %>');
然后您必须在您的页面上创建某种计时器来重复调用该控制器操作,我使用 jquery.timers。在计时器循环中,您会调用
$.get('/get_file_contents?file_to_read=public/logfile');
这将访问控制器,获取文件内容,并执行 get_file_contents.js.erb,这将使用文件的当前内容更新 div。
您必须将路由 /get_file_contents 添加到 routes.rb。