我对ROR很新,我偶然发现了一个问题。我认为这是一个新手问题,但我不确定是不是。我建立这个应用程序是用户应该能够输入他们的业务中的纸张使用量,并从那里应用程序计算他们的生态足迹,所以说。
在登录时,用户输入他们的员工编号。工作人员编号用于分隔paper_weight等(参见视图示例)。
我知道它基本上按现在的方式工作,但我知道这不是最好的做法。
我还想将包含运行总计的div移动到application.html.erb,但我不能这样做,因为我的计算是在View中而不是在Model中。
这是指向github repo https://github.com/DadiHall/SprettaEMS1.5
的链接还有其他方法吗?有人可以告诉我吗?
我的index.html.erb视图
<div class="row">
<div class="col-md-10 ">
<h1>Pappírsnotkun</h1>
<table class=" well table table-hover">
<thead>
<tr>
<th>Dags</th>
<th>Tegund</th>
<th>Þyngd</th>
<th>Kostnaður</th>
<th>Þar af umhvm. pappír</th>
<th>Pappírs magn m.v. Stöðugildi</th>
<th>Hl.f. Umhvm. Pappírs</th>
<th>Fjöldi trjáa m.v. magn pappírs</th>
</tr>
</thead>
<tbody>
<% paper_total_cost = 0 %>
<% paper_total_trees = 0 %>
<% paper_total_staff_ratio = 0 %>
<% @papers.each do |paper| %>
<tr>
<td><%= paper.date.strftime("%Y/%m/%d") %></td>
<td><%= paper.paper_type %></td>
<td><%= paper.paper_weight %> -kg.</td>
<td><%= paper.paper_cost %> -kr.</td>
<td><%= paper.env_paper_weight %> -kg.</td>
<td><%= (paper.paper_weight.to_i / current_user.staff) %> kg/stöðugildi </td>
<td><%= (( paper.env_paper_weight / paper.paper_weight)* 100) %>%</td>
<td><%= (( paper.paper_weight.to_f/1000)*15) %></td>
<td><%= link_to 'Sýna', paper, class: 'btn btn-xs btn-info' %></td>
<td><%= link_to 'Uppfæra', edit_paper_path(paper), class: 'btn btn-xs btn-warning' %></td>
<td><%= link_to 'Eyða', paper, method: :delete, class: 'btn btn-xs btn-danger', data: { confirm: 'Are you sure?' } %></td>
<% paper_total_cost = paper_total_cost + (paper.paper_cost.to_i) %>
<% paper_total_trees = paper_total_trees + (( paper.paper_weight.to_f/1000)*15) %>
<% paper_total_staff_ratio = paper_total_staff_ratio + (paper.paper_weight.to_i / current_user.staff) %>
</tr>
<% end %>
</tbody>
</table>
</div>
<div class="row">
<div class=" col-md-2 panel panel-default pull-right" >
<div class="panel panel-heading ">
<h5 class="user-name">User: <% if current_user && current_user %>
<%= current_user.name %>,
Staff: <%= current_user.staff %>
<% end %>
</h5>
</div>
<div class="panel-body">
<h6 class="pull-right">Pappír Kostnaður Samtals:<%= number_with_precision(paper_total_cost.to_f, precision: 2) %> kr </h6><br>
<h6 class="pull-right">Fjöldi Trjáa vegna Pappírs notkunar:<%= number_with_precision(paper_total_trees.to_f, precision: 2) %> Tré </h6><br>
<h6 class="pull-right">Magn Pappírs f. hvert stöðugildi:<%= number_with_precision(paper_total_staff_ratio.to_f, precision: 2) %> Kg </h6>
</div>
</div>
</div>
<%= link_to 'New Paper', new_paper_path %>
我的papers_controller.rb
class PapersController < ApplicationController
before_action :set_paper, only: [:edit, :update, :show, :destroy]
def index
@papers = Paper.all
end
def create
@paper = Paper.new(paper_params)
if @paper.save
flash[:success] = "Messages sent."
redirect_to paper_path(@paper)
else
flash[:danger] = "Error occured, message has not been sent."
redirect_to new_paper_path
end
end
def new
@paper = Paper.new
end
def edit
end
def show
end
def update
if @paper.update(paper_params)
flash[:success] = "Line was successfully updated"
redirect_to papers_path(@paper)
else
render 'edit'
end
end
def destroy
@paper.destroy
flash[:danger] = "Line was successfully destroyed"
redirect_to papers_path
end
private
def set_paper
@paper = Paper.find(params[:id])
end
def paper_params
params.require(:paper).permit(:paper_type, :date, :paper_weight, :paper_cost, :env_paper_weight)
end
end
my paper.rb Model
class Paper < ActiveRecord::Base
has_many :users
end
答案 0 :(得分:2)
您可以通过定义模型中的函数将计算移动到模型中。
第一个计算使用current_user,默认情况下不在模型中,因此必须是参数。另外两个只是使用纸张的属性;请原谅我糟糕的命名,我不清楚后两个指标是什么。
class Paper
has_many :users
def paper_weight_per_capita(current_user)
paper_weight.to_i / current_user.staff
end
def paper_weight_metric1
( env_paper_weight / paper_weight)* 100)
end
def paper_weight_metric2
(( paper_weight.to_f/1000)*15)
end
end
在你看来
<%= paper.paper_weight_per_capita(current_user) %>
<%= paper.paper_weight_metric1 %>
如果您想要布局中的运行总计,那么您需要在每个页面上加载纸质对象。您可以使用application_controller before_action执行此操作,但some folks don't like this approach和here,但我认为这对您来说是合理的方式。
class ApplicationController < ActionController::Base
before_action :load_running_totals
def load_running_totals
# method 1. Similar to how the calculation is done in the view.
# @paper_weight_per_capita = Papers.all.map(&:paper_weight_per_capita, current_user).reduce(&:+)
# method 2. does the summation in the database without having to load all the records into ruby. (weight needs to be a number in the db.)
@paper_weight_per_capita = Papers.sum(:weight) / current_user.staff
end
end
现在在任何视图(包括你的布局)中:
<%= @paper_weight_per_capita %>