每个项目的数量和价格如何相乘

时间:2016-01-18 20:53:48

标签: ruby-on-rails ruby-on-rails-3 ruby-on-rails-4 ruby-on-rails-3.2 ruby-on-rails-3.1

在Rails中,无法乘以模型文件中使用的每个项目的数量x价格。如何在Ruby中将数量x价格相乘,而且,如果我能够以javascript为导向,以每个项目的总和实时显示价格和数量。另外,使用erubis可以实时使用ruby代码。我附上详细信息:

ticket.rb

class Ticket < ActiveRecord::Base
  validates_presence_of :fecha, :customer_id, :status_id, 
  :impuesto, :moneda, :numero_boleta

  belongs_to :customer
  belongs_to :status
  belongs_to :sale
  has_many :detail_tickets, :dependent => :destroy

  accepts_nested_attributes_for :detail_tickets, :allow_destroy => true

  attr_accessible :fecha, :customer_id, :status_id, 
  :impuesto, :moneda, :numero_boleta, :detail_tickets_attributes

  def totalprice
    valor = 0.0
    self.detail_tickets.each do |p|
        valor = p.cantidad * p.price
    end
  end
end

detail_ticket.rb

class DetailTicket < ActiveRecord::Base
  attr_accessible :item, :code, :cantidad, :description, :price
  validates_presence_of :item, :code, :cantidad, :description, :price

  belongs_to :ticket
end

form.html.erb

 <%= simple_form_for @ticket do |f| %>
      <%= f.error_notification %>
        <div class="form-inputs">

        <table>
            <tr>
                <th class="text-center" width="450px"><%= f.input :fecha, label: 'Fecha' %></th>
                <%= f.input :impuesto, input_html: { value: '18.00'}, as: :hidden %>
                <th class="text-center" width="450px"><%= f.input :moneda, collection: ["S/.", "US$"], label: 'Tipo de Moneda' %></th>
            </tr>
        </table>

        <table>
            <tr>
                <th class="text-center" width="450px"><%= f.association :customer, label_method: "#{:nombre}", value_method: :id, prompt: "Debes buscar la empresa", error: 'empresa' %></th>
                <th class="text-center" width="450px"><%= f.association :status %></th>
            </tr>
        </table>

        <table>
            <tr>
                <th class="text-center" width="900px"><%= f.input :numero_boleta, label: 'Numero de Boleta', value: :id %></th>
            </tr>
        </table>
        <div>

        <table id="items">
              <tr>
                <th class="text-center" width="60px">ITEM</th>
                <th class="text-center" width="160px">CODIGO</th>
                <th class="text-center" width="225px">DESCRIPCION</th>
                <th class="text-center" width="100px">CANTIDAD</th>
                <th class="text-center" width="110px">PRECIO</th> 
                <th class="text-center" width="150px">TOTAL <%= @empresa.moneda1 %></th>
                <th></th>
              </tr>
        </table>
        <% @totales = 0.00 %>   
    <%= simple_nested_form_for @ticket, :wrapper => false do |g| %>
        <table id="detail_tickets">
        <%= g.simple_fields_for :detail_tickets do |p| %>
        <tr class="fields">
            <th align="center" width="60px" class="text-center"><%= p.input :item, label: false %></th>
            <th align="center" width="160px"><%=h p.input :code, label: false %></th>
            <th align="center" width="225px" class="description"><%=h p.input :description, label: false, input_html: {:rows => 3} %></th>
            <th id="qty" align="center" width="100px" class="text-center"><%=h p.input :cantidad, label: false %></th>
            <th id="price" align="center" width="110px" class="text-right"><%=h p.input :price, label: false %></th>
            <th align="right" width="150px" class="text-right"><%= number_with_precision(@ticket.totalprice, :delimiter => ',', :separator => '.') %></th>
            <th align="center" width="63px" class="text-center"><%= p.link_to_remove "", class: "btn btn-danger fa fa-trash" %></th>

enter image description here

0 个答案:

没有答案