如何将数据发送到我的模型以进行逻辑处理并将某些变量中的数据发送回控制器以便它返回json?
我尝试在下面做同样的事,但我没有成功,我有以下信息:
NoMethodError:未定义的方法`infected ='对于 类:0x00007f8d780d5d10
我试图检索我的模型变量的感染,未感染和point_lost。
控制器:
def index
@survivor = Survivor.all
@survivor_infected = Survivor.where(infected: true)
@survivor.estimative (@survivor_infected)
render json: { infected: "#{@survivor.infected}%",
non_infected: "#{}%",
points_lost: ""
}
end
我的模特:
def self.estimative (s_infected)
@survivor_infected = s_infected
@water = []
@water_price = []
@food = []
@food_price = []
@medication = []
@medication_price = []
@ammunition = []
@ammunition_price = []
@survivor_infected.each do |s|
@water << s.inventory.items.where(item: "Water").pluck(:quantity).join.to_i
@water_price << s.inventory.items.where(item: "Water").pluck(:price).join.to_i
@food << s.inventory.items.where(item: "Food").pluck(:quantity).join.to_i
@food_price << s.inventory.items.where(item: "Food").pluck(:price).join.to_i
@medication << s.inventory.items.where(item: "Medication").pluck(:quantity).join.to_i
@medication_price << s.inventory.items.where(item: "Medication").pluck(:price).join.to_i
@ammunition << s.inventory.items.where(item: "Ammunition").pluck(:quantity).join.to_i
@ammunition_price << s.inventory.items.where(item: "Ammunition").pluck(:price).join.to_i
end
self.infected = ((@survivor_infected.count * 100)/(self.count))
non_infected =((@survivor_infected.rewhere(infected: false).count * 100)/(self.count))
points_lost = ( @water.reduce(:+) * @water_price.reduce(:+)) + (@food.reduce(:+) * @food_price.reduce(:+)) + (@medication.reduce(:+) * @medication_price.reduce(:+)) + (@ammunition.reduce(:+) * @ammunition_price.reduce(:+))
end
有人可以帮忙吗?
答案 0 :(得分:0)