controller:
def get_host_status_api
@server=Server.find_by_openid(session[:openid])
@hosts = @server.hosts
@hosts.each do |host|
if @server.skip_check
host[running]='unknown'
else
get_host_status(host.ip).each_pair {|name, val| host[name]=val}
end
end
respond_to do |format|
format.json { render :json => @hosts }
end
end
get_host_status 返回第三方接口的数据,形如{"cpu"=>"2%","mem"=>"5%"} model:
class Host < ActiveRecord::Base
attr_accessible :ip, :nick, :server_id
validates :ip,
:length => {:minimum=>7,:maximum=>15},
:uniqueness => true,
:format => {:with=> /\d+\.\d+\.\d+\.\d+/ }
belongs_to :server
end
看起来返回是正确的:
[{"cpu":"0.2%","created_at":"2013-07-05T17:51:53+08:00","id":126,"ip":"192.168.1.101","mem":"25%","nick":"debian\n","running":"idle","server_id":5,"updated_at":"2013-07-05T17:51:53+08:00","uptime":"1 days, 2:21","version":"20130612"},{"created_at":"2013-07-12T10:57:17+08:00","id":127,"ip":"8.8.8.8","nick":null,"running":"unknown","server_id":5,"updated_at":"2013-07-12T10:57:17+08:00"}]
但日志报错:
DEPRECATION WARNING: You're trying to create an attribute `running'. Writing arbitrary attributes on a model is deprecated. Please just use `attr_writer` etc. (called from block (3 levels) in get_host_status_api at /Users/hellomac/Desktop/power/app/controllers/servers_controller.rb:132)
DEPRECATION WARNING: You're trying to create an attribute `version'. Writing arbitrary attributes on a model is deprecated. Please just use `attr_writer` etc. (called from block (3 levels) in get_host_status_api at /Users/hellomac/Desktop/power/app/controllers/servers_controller.rb:132)
DEPRECATION WARNING: You're trying to create an attribute `cpu'. Writing arbitrary attributes on a model is deprecated. Please just use `attr_writer` etc. (called from block (3 levels) in get_host_status_api at /Users/hellomac/Desktop/power/app/controllers/servers_controller.rb:132)
DEPRECATION WARNING: You're trying to create an attribute `mem'. Writing arbitrary attributes on a model is deprecated. Please just use `attr_writer` etc. (called from block (3 levels) in get_host_status_api at /Users/hellomac/Desktop/power/app/controllers/servers_controller.rb:132)
DEPRECATION WARNING: You're trying to create an attribute `uptime'. Writing arbitrary attributes on a model is deprecated. Please just use `attr_writer` etc. (called from block (3 levels) in get_host_status_api at /Users/hellomac/Desktop/power/app/controllers/servers_controller.rb:132)
按照错误信息我尝试修改 controller:
def get_host_status_api
@server=Server.find_by_openid(session[:openid])
@hosts = @server.hosts
@hosts.each do |host|
if @server.skip_check
==> host.running='unknown'
else
==> get_host_status(host.ip).each_pair {|name, val| host.send("#{name}=",val)}
end
end
respond_to do |format|
format.json { render :json => @hosts }
end
end
model:
class Host < ActiveRecord::Base
attr_accessible :ip, :nick, :server_id
==> attr_accessor :running, :version, :cpu, :mem, :uptime
validates :ip,
:length => {:minimum=>7,:maximum=>15},
:uniqueness => true,
:format => {:with=> /\d+\.\d+\.\d+\.\d+/ }
belongs_to :server
end
报错信息是没了,但返回的结果为:
[{"created_at":"2013-07-05T17:51:53+08:00","id":126,"ip":"192.168.1.101","nick":"debian\n","server_id":5,"updated_at":"2013-07-05T17:51:53+08:00"},{"created_at":"2013-07-12T10:57:17+08:00","id":127,"ip":"8.8.8.8","nick":null,"server_id":5,"updated_at":"2013-07-12T10:57:17+08:00"}]
这种情况到底应该怎么处理呢???