所以,我有一个名为“Status Updates Helper”的助手和一个名为“Meals Helper”的助手。
我需要从Meals Helper ...中访问Status Updates Helper中的变量...
我尝试在我的Meals Helper中使用“include StatusUpdatesHelper”,虽然这似乎不起作用。
这是我的膳食帮助文件:
module MealsHelper
def total_of(macro)
current_user.meal_foods.map(¯o).inject(:+)
end
def pct_fat_satisfied
#how much of a macro is needed?
# fat_needed = fat factor * current lbm
fat_factor = current_user.fat_factor
current_lbm = current_user.status_update.first.current_lbm
fat_needed = fat_factor * current_lbm
#how much is in the meal?
fat_provided = total_of(:fat)
#percent needed
pct_fulfilled = fat_provided.to_f/fat_needed.to_f
return BigDecimal(pct_fulfilled, 2)*100
end
def pct_carbs_satisfied( tdee, deficit_pct )
#how many carbs are needed?
cals_needed = tdee.to_f * (1 - deficit_pct.to_f)
carbs_needed = cals_needed * 4
#how many carbs are provided?
carbs_provided = total_of(:carbs)
#what is the pct satisfied?
pct_fulfilled = carbs_provided.to_f/carbs_needed.to_f
return tdee
end
def pct_protein_satisfied
#how much protien is needed?
protein_factor = current_user.protein_factor
current_lbm = current_user.status_update.first.current_lbm
protein_needed = protein_factor * current_lbm
#how much protien is provided?
protein_provided = total_of(:protien)
#pct of protien satisfied?
pct_fulfilled = protein_provided.to_f/protein_needed.to_f
return BigDecimal(pct_fulfilled, 2)*100
end
end
这是状态更新帮助文件:
module StatusUpdatesHelper
def bmr(lbm)
lbm *= 0.45
return '%.2f' % (370 + (21.6 * lbm.to_d))
end
def target_weight(total_weight, target_bf_pct, lbm)
target_bf_pct /= 100
return '%.2f' % ((total_weight*target_bf_pct)+lbm)
end
def fat_to_burn(total_weight, target_weight)
return '%.2f' % (total_weight.to_d - target_weight.to_d)
end
def tdee(bmr, activity_factor)
return '%.2f' % (bmr.to_d*activity_factor.to_d)
end
def deficit_pct(deficit_amnt, tdee)
daily_cal_def = ((deficit_amnt.to_f * 3500)/7)
return (daily_cal_def.to_d/tdee.to_d)
end
def daily_calorie_target(tdee, deficit_pct)
return '%.2f' % (tdee.to_d * deficit_pct.to_d)
end
def weekly_burn_rate(tdee, daily_calorie_target)
return '%.2f' % (daily_calorie_target.to_d*7)
end
def time_to_goal(weekly_burn_rate, fat_to_burn)
return '%.2f' % (fat_to_burn.to_d*3500/weekly_burn_rate.to_d)
end
def daily_intake( tdee, daily_calorie_target )
return '%.2f' % (tdee.to_d - daily_calorie_target.to_d)
end
def total_progress
if user_signed_in?
if current_user.status_update.empty?
@total_weight_change = 0
@total_fat_change = 0
@total_lbm_change = 0
@time_to_goal = 0
@fat_to_burn = 0
@target_bf_pct = 0
@lbm = 0
@activity_factor = 0
@bmr = 0
@total_weight = 0
@target_weight = 0
@fat_to_burn = 0
@tdee = 0
@deficit_amnt = 0
@deficit_pct = 0
@daily_calorie_target = 0
@daily_intake = 0
@weekly_burn_rate = 0
@time_to_goal = 0
@current_weight = 0
@current_bf_pct = 0
@current_lbm = 0
@current_fat_weight = 0
@daily_caloric_deficit = 0
end
if current_user.status_update.any?
@first = current_user.status_update.first
@last = current_user.status_update.last
@beginning_date = current_user.status_update
.first.created_at.strftime("%m/%d/%Y")
@last_date = current_user.status_update
.last.created_at.strftime("%m/%d/%Y")
@total_weight_change = BigDecimal(@first.current_weight -
@last.current_weight, 3)
@total_fat_change = BigDecimal(@first.current_fat_weight -
@last.current_fat_weight, 3)
@total_lbm_change = BigDecimal(@first.current_lbm -
@last.current_lbm, 3)
@recent_fat_change = BigDecimal(@first.current_fat_weight -
@first.previous_status_update.current_fat_weight, 3)
@recent_lbm_change = BigDecimal(@first.current_lbm -
@first.previous_status_update.current_lbm, 2)
@recent_weight_change = BigDecimal(@first.current_weight -
@first.previous_status_update.current_weight, 2)
@lbm = @first.current_lbm
@activity_factor = current_user.activity_factor
@bmr = bmr(@lbm)
@total_weight = @first.current_weight
@target_bf_pct = (current_user.target_bf_pct)
@target_weight = target_weight(@total_weight, @target_bf_pct, @lbm)
@fat_to_burn = fat_to_burn(@total_weight, @target_weight)
@tdee = tdee(@bmr, @activity_factor)
@deficit_amnt = current_user.deficit_amnt
@deficit_pct = deficit_pct(@deficit_amnt, @tdee)
@daily_calorie_target = daily_calorie_target(@tdee, @deficit_pct)
@daily_intake = daily_intake(@tdee, @daily_calorie_target)
@weekly_burn_rate = weekly_burn_rate(@tdee, @daily_calorie_target)
@time_to_goal = time_to_goal(@weekly_burn_rate, @fat_to_burn)
@current_weight = BigDecimal(@first.current_weight, 4)
@current_bf_pct = BigDecimal(@first.current_bf_pct * 100, 4)
@current_lbm = BigDecimal(@first.current_lbm, 4)
@current_fat_weight = BigDecimal(@first.current_fat_weight, 4)
@daily_caloric_deficit = @tdee.to_d - @daily_intake.to_d
#End Date
@start_date = current_user.status_update.first.created_at
@end_date = (@start_date + @time_to_goal.to_i.weeks).strftime("%m/%d/%Y")
end
end
end
end
从用餐节目视图中,当我要求pct_carbs_fulfilled时,它返回“无穷大”。
如果我把“100”归入他返回部分,它将返回“100”。
状态更新助手工作得很好。
我知道这不是一个宁静的策略,因此非常感谢任何有关如何让它变得更加安宁的建议。
所以我的问题是,为什么我不工作,如何将这些变量传递到餐饮节目视图?
以下是用于显示变量的膳食显示视图的一部分
<td> <%= pct_fat_satisfied %>% </td>
<td> <%= @tdee %>% </td>
<td> <%= pct_protein_satisfied %>% </td>
谢谢!
答案 0 :(得分:0)
我意识到我需要将所有这些逻辑移到用户模型中,这就是我所做的。
我删除了整个“total_progress”方法,并在其中的每个变量中创建了实例方法。
我没有尝试将所有这些变量保存在一个对象中,而是发现我可以使用从其他对象提供的新数据和属于该用户的相对静态属性来解决任何使用这些方法的属性。
这使得所有其他“计算”变量更加动态,因为我永远不必保存它们。每次用户更新其他对象时,这些值会立即更新,因为找到它们需要每次都重新计算它们。
我发现在模型中放置尽可能多的逻辑,同时保持我的控制器很薄,我的观点也没有任何逻辑,这是非常有用的。
它使生活更轻松9,000,000,002,839,420,934,820,394倍。
故事的寓意是“使用胖模型和瘦控制器”。