我是初学者,我想知道在Rails中应该将这样的计算放在哪里以及如何显示它:
<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:p="http://primefaces.org/ui"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:c="http://java.sun.com/jsp/jstl/core">
<h:head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<link href="//netdna.bootstrapcdn.com/font-awesome/4.0.3/css/font-awesome.css" rel="stylesheet"></link>
<title>Insert title here</title>
</h:head>
<h:body>
<ui:debug />
<h:form id="formulaire1">
<h:inputHidden value="#{listeOdm.n_odm}" />
<h:inputHidden value="#{listeOdm.operation}" />
<p:growl id="msgs" showDetail="true"/>
<h:dataTable id="basicDT" var="odm" value="#{listeOdm.listOdm}" binding="#{listeOdm.dataTable}" >
<h:column headerText="Etat" id= "bulet">
<h:panelGroup rendered="#{odm.valide_manag1 eq '2'}">
<h:graphicImage library="img" name="if_bullet-green_44189.png" width="24" height="24" style="border-style: none"/>
</h:panelGroup>
<h:panelGroup rendered="#{odm.valide_manag1+odm.valide_manag2 eq '1'}">
<h:graphicImage library="img" name="orange.gif" width="24" height="24" style="border-style: none"/>
</h:panelGroup>
<h:panelGroup rendered="#{odm.valide_manag1+odm.valide_manag2 eq '0'}">
<h:graphicImage library="img" name="if_bullet-red_44191.png" width="24" height="24" style="border-style: none"/>
</h:panelGroup>
</h:column>
<h:column headerText="valid">
<h:outputText value="#{odm.valide_manag1}" />
</h:column>
<h:column headerText="Num Odm">
<h:outputText value="#{odm.num_odm}" />
</h:column>
<h:column headerText="Client">
<h:outputText value="#{odm.client}" />
</h:column>
<h:column headerText="Station">
<h:outputText value="#{odm.station}" />
</h:column>
<h:column headerText="Destination">
<h:outputText value="#{odm.destination}" />
</h:column>
<h:column headerText="date dep">
<h:outputText value="#{odm.date_dep}" pattern="dd/MM/yyyy"/>
</h:column>
<h:column headerText="date retour">
<h:outputText value="#{odm.date_ret}" pattern="dd/MM/yyyy"/>
</h:column>
<h:column headerText="Cient">
<h:outputText value="#{odm.client}" />
</h:column>
<h:column headerText="Edit" width="24">
<h:commandLink
actionListener="#{listeOdm.editOdm}" update=":formulair1:basicDT" ajax="true">
<f:param name="numOdm" value="#{odm.num_odm}"></f:param>
<f:param name="operation" value="edit"></f:param>
<h:graphicImage library="img" name="118805-128.png" width="24" height="24" style="border-style: none"/>
<!-- <i class="fa fa-envelope"></i> -->
</h:commandLink>
<h:commandLink
actionListener="#{listeOdm.printOdm}" update="formulaire1">
<f:param name="numOdm" value="#{odm.num_odm}"></f:param>
<f:param name="operation" value="edit2"></f:param>
<h:graphicImage library="img" name="Print-2-128.png" width="24" height="24" style="border-style: none"/>
<!-- <i class="fa fa-envelope"></i> -->
</h:commandLink>
</h:column>
</h:dataTable>
</h:form>
</h:body>
</html>
以下是我所指的表格:
@suplement.total_cost = @suplement.number_of_days * @suplement.daily_dosage_in_units * (@suplement.suplement_cost / @suplement.number_of_units)
虽然我只是在视图中尝试此计算,但它正确计算并显示在create_table "suplements", force: :cascade do |t|
t.string "name"
t.integer "number_of_units"
t.integer "daily_dosage_in_units"
t.float "suplement_cost"
t.float "total_cost"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "user_id"
t.integer "number_of_days"
end
中,但显然这不是我想要的。这是我想要的方式,但它显示除total_cost之外的所有内容:
f.text_area :total_cost
答案 0 :(得分:2)
您可以在其类中定义total_attribute
,Suplement。这样您就可以访问所有剩余的属性,总数是“计算的”属性:
class Suplement < ApplicationRecord
def total_cost
suplement_over_units = suplement_cost / number_of_units
dosage_per_days = number_of_days * daily_dosage_in_units
dosage_per_days * suplement_over_units
end
end
刚刚分配了一些变量,如您的示例所示,它只是访问属性,因为您无法访问@suplement
变量:
def total_cost
number_of_days * daily_dosage_in_units * (suplement_cost / number_of_units)
end