【发布时间】:2018-04-11 17:21:24
【问题描述】:
我的调查(用水)的控制器出现问题。它从一个表格中收集了 30 多个变量,这些输入需要保存到 waterusage db 并用于计算最终分数,也保存在数据库中。
class Waterusage < ApplicationRecord
belongs_to :user
end
class WaterusagesController < ApplicationController
def new
@waterusage = Waterusage.new
end
def create
@user = User.find(params[:user_id])
_showerTotal = :average_shower * :shower_flow_rate * :household_size
_bathTotal = :bath_rate * :bath_multiplier * 35
_bathroomSinkTotal = :bathroom_sink_usage * :bathroom_sink_flow_rate * :household_size
_toiletTotal = :mellow * :low_flow_toilet * :household_size
_kitchenTotal = :kitchen_sink_usage * :kitchen_sink_flow_rate
_dishwashingTotal = :dishwasher_rate * :dishwasher_multiplier * :dishwasher_method
_laundryTotal = :laundry_rate * :laundry_method * :laundry_multiplier
_homeUsage = _showerTotal + _bathTotal + _bathroomSinkTotal + _toiletTotal + _kitchenTotal + _dishwashingTotal + _laundryTotal + :greywater
_lawnTotal = :lawn_rate * :lawn_multiplier * :lawn_size * :xeriscaping
_swimmingTotal = (:swimming_pool / 365) + (:swimming_months * 1000 / 365
_carwashTotal = :carwash_rate * :carwash_multiplier * :carwash_method
_outsideUsage = _lawnTotal + _swimmingTotal + _carwashTotal
_drivingTotal = 0.735 * :miles
_powerTotal = :statewater * :percent_statewater / 100
_indirectTotal = :shopping + :paper_recycling + :plastic_recycling + :can_recycling + :textile_recycling + :diet + (200 * :pet_cost / 30)
:household_total = _homeUsage + _outsideUsage + _drivingTotal + _powerTotal + _indirectTotal
:individual_total = :household_total / :household_size
@waterusage = @user.waterusage.create(waterusage_params)
redirect_to user_path(@user)
end
def destroy
@user = User.find(params[:user_id])
@waterusage = @user.waterusage.find(params[:id])
@waterusage.destroy
redirect_to user_path(@user)
end
private
def waterusage_params
params.require(:waterusage).permit(:household_size, :average_shower,
:shower_flow_rate, :bath_rate, :bath_multiplier, :bathroom_sink_usage,
:bathroom_sink_flow_rate, :mellow, :low_flow_toilet, :kitchen_sink_usage,
:kitchen_sink_flow_rate, :dishwasher_rate, :dishwasher_multiplier,
:dishwasher_method, :laundry_rate, :laundry_multiplier, :laundry_method,
:greywater, :lawn_rate, :lawn_multiplier, :lawn_size, :xeriscaping,
:swimming_pool, :swimming_months, :carwash_rate, :carwash_multiplier,
:carwash_method, :miles, :statewater, :percent_statewater, :shopping,
:paper_recycling, :plastic_recycling, :can_recycling, :textile_recycling,
:diet, :pet_cost, :individual_total, :household_total)
end
end
有没有更好的方法可以做到这一点?目前,在汇总小计的行上存在错误。 (即:household_total = _homeUsage + _outsideUsage + _drivingTotal + _powerTotal + _indirectTotal ) 另外我不确定我是否将用户信息正确连接到调查架构
【问题讨论】:
-
这里有未闭合的括号吗:
(:swimming_months * 1000 / 365? -
哦,另外,我想我会很想将所有原始变量存储在
WaterUsage模型中,然后将所有数学运算转移到decorator中。 -
这在我看来不像是有效的 Ruby 代码。您正在使用局部变量之类的符号。即使代码有效,您也不会对结果做任何事情。
标签: ruby-on-rails database activerecord