Rails: 在将控制器保存到数据库之前,如何在控制器的创建操作中使用表单输入变量进行计算?
Rails: How do I make a calculation using form input variables in the create action of a controller before saving it in the db?
在我的表单中,用户输入 his/her 体重和体脂百分比。在我的控制器创建操作中,我想计算人的瘦体重并将其保存到我的数据库中的 lean_mass 列。
lean_mass = weight * (1 - body_fat)
schema.rb
create_table "stats", force: true do |t|
t.integer "user_id"
t.integer "weight"
t.decimal "body_fat"
t.integer "lean_mass"
t.date "date"
t.datetime "created_at"
t.datetime "updated_at"
end
stats/new.html.erb
<%= form_for @stat do |f| %>
<%= f.label :date %>
<%= f.date_field :date %>
<%= f.label :weight %>
<%= f.number_field :weight %>
<%= f.label :body_fat %>
<%= f.number_field :body_fat, step: 0.1 %>
<%= f.submit %>
<% end %>
stats_controller.rb
class StatsController < ApplicationController
before_action :authenticate_user! #I'm using devise
def index
@stats = current_user.stats
end
def new
@stat = current_user.stats.build
end
def create
##
###How do I calculate and add lean_mass to stat_params here?###
##
@stat = current_user.stats.build(stat_params)
if @stat.save
redirect_to root_url, notice: "Stat was successfully created."
else
redirect_to dashboard_url, notice: "Problem creating stat."
end
end
private
def stat_params
params.require(:stat).permit(:date, :weight, :body_fat)
end
end
我需要有关创建操作的帮助。
手动计算并插入瘦体重:
您必须先计算瘦体重:
lean_mass = params[:stat][:weight].to_i * (1 - params[:stat][:body_fat].to_i)
然后你可以将它添加到新的@stat
对象中
@stat = current_user.stats.build(stat_params.merge(:lean_mass => lean_mass))
# OR
@stat.lean_mass = lean_mass
然后您可以继续保存对象。
自动处理瘦体重
假设您像这样验证 weight
和 body_fat
列的存在:
stat.rb
validates_presense_of :weight, :body_fat
因为 lean mass
总是依赖于 weight
& body_fat
我建议两件事:
1) 您可以在模型中添加方法 lean_mass
returns 计算值,这样就不再需要该列了。
stat.rb
def lean_mass
self.weight * (1 - self.body_fat)
end
2) 添加一个 after_save
callback 自动计算和存储 lean_mass
值
stat.rb
after_save { self.update(:lean_mass => self.weight * (1 - self.body_fat)) }
强烈建议使用其中一种解决方案,因为您不必担心 create
或 update
操作中的 lean mass
列。
在我的表单中,用户输入 his/her 体重和体脂百分比。在我的控制器创建操作中,我想计算人的瘦体重并将其保存到我的数据库中的 lean_mass 列。
lean_mass = weight * (1 - body_fat)
schema.rb
create_table "stats", force: true do |t|
t.integer "user_id"
t.integer "weight"
t.decimal "body_fat"
t.integer "lean_mass"
t.date "date"
t.datetime "created_at"
t.datetime "updated_at"
end
stats/new.html.erb
<%= form_for @stat do |f| %>
<%= f.label :date %>
<%= f.date_field :date %>
<%= f.label :weight %>
<%= f.number_field :weight %>
<%= f.label :body_fat %>
<%= f.number_field :body_fat, step: 0.1 %>
<%= f.submit %>
<% end %>
stats_controller.rb
class StatsController < ApplicationController
before_action :authenticate_user! #I'm using devise
def index
@stats = current_user.stats
end
def new
@stat = current_user.stats.build
end
def create
##
###How do I calculate and add lean_mass to stat_params here?###
##
@stat = current_user.stats.build(stat_params)
if @stat.save
redirect_to root_url, notice: "Stat was successfully created."
else
redirect_to dashboard_url, notice: "Problem creating stat."
end
end
private
def stat_params
params.require(:stat).permit(:date, :weight, :body_fat)
end
end
我需要有关创建操作的帮助。
手动计算并插入瘦体重:
您必须先计算瘦体重:
lean_mass = params[:stat][:weight].to_i * (1 - params[:stat][:body_fat].to_i)
然后你可以将它添加到新的@stat
对象中
@stat = current_user.stats.build(stat_params.merge(:lean_mass => lean_mass))
# OR
@stat.lean_mass = lean_mass
然后您可以继续保存对象。
自动处理瘦体重
假设您像这样验证 weight
和 body_fat
列的存在:
stat.rb
validates_presense_of :weight, :body_fat
因为 lean mass
总是依赖于 weight
& body_fat
我建议两件事:
1) 您可以在模型中添加方法 lean_mass
returns 计算值,这样就不再需要该列了。
stat.rb
def lean_mass
self.weight * (1 - self.body_fat)
end
2) 添加一个 after_save
callback 自动计算和存储 lean_mass
值
stat.rb
after_save { self.update(:lean_mass => self.weight * (1 - self.body_fat)) }
强烈建议使用其中一种解决方案,因为您不必担心 create
或 update
操作中的 lean mass
列。