1

I need your support with ruby on rails. I'm trying to calculate the result of price and quantity in the light of new and edit.En view show if it works. I'm using Simple_form, calculate input quantity and price. automatically and must leave the result. sending the code.

<%= simple_form_for(@ticket) do |f| %>
  <%= f.error_notification %>
    <div class="form-inputs">
    <%= f.input :fecha %>
    <%= f.input :impuesto, :input_html => { value: '18.00'} %>
    <%= f.association :customer, label_method: "#{:nombre}", value_method: :id, prompt: "Debes buscar la empresa" %>
    <%= f.association :status %>
    <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>    
<%= 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"><%= p.input :code, label: false %></th>
        <th align="center" width="225px" class="description"><%= p.input :description, label: false, :input_html => {:rows => 3} %></th>
        <th align="center" width="100px" class="text-center"><%= p.input :cantidad, label: false %></th>
        <th align="center" width="110px" class="text-right"><%= p.input :price, label: false %></th>
        <% @total_price = params[:cantidad].to_s.to_d * params[:price].to_s.to_d %> 
        <th align="right" width="150px" class="text-right"><%= @total_price %></th>
        <th align="center" width="63px" class="text-center"><%= p.link_to_remove "", class: "btn btn-danger fa fa-trash" %></th>
    </tr>
    <% end %>
    </table>

    <table id="items">
        <tr>
            <th width="700px"></th>
            <th class="text-center" width="110px">VALOR DE VENTA : </th> 
            <th class="text-center" width="150px"></th> 
        </tr>

        <tr>
            <th width="700px"></th>
            <th class="text-center" width="110px">I.G.V. : </th> 
            <th class="text-center" width="150px"></th> 
        </tr>

        <tr>
            <th width="700px"></th>
            <th class="text-center" width="110px">TOTAL : </th> 
            <th class="text-center" width="150px"></th> 
        </tr>
    </table>
    <br>
    <br>
    <p><%= g.link_to_add "Adicionar Producto", :detail_tickets, :data => { :target => "#detail_tickets" }, class: "btn btn-primary" %></p>
  <div class="form-actions">
    <%= f.button :submit %>
  </div>
<% end %>
<% end %>
Croelanjr
  • 9
  • 5
  • What error message do you get or what should your code do which its not doing? – Sahil Dec 20 '15 at 01:44
  • <% @total_price = params[:cantidad].to_s.to_d * params[:price].to_s.to_d %> – Croelanjr Dec 20 '15 at 02:25
  • do not get the parameter data. – Croelanjr Dec 20 '15 at 02:25
  • Are you passing the parameters `:cantidad` and `:price` to the page, try printing them in console, using `<% puts "\ncantidad #{params[:cantidad]}, price: #{params[:price]} \n" %>`. May I ask why are you converting the params to string and then to big data-decimal? – Sahil Dec 20 '15 at 02:35
  • cantidad , price: nothing comes!!!! – Croelanjr Dec 20 '15 at 03:04
  • Processing by TicketsController#create as HTML Parameters: {"utf8"=>"✓", "authenticity_token"=>"3IquFKLKLbbk7w85O+FbFrqhFdDeiUjt6HK/6Kq/Lwt4nAIct6MPOUvnv1r7DBSMWnbfwGO9ew8VnjFu19ImfQ==", "ticket"=>{"fecha(3i)"=>"20", "fecha(2i)"=>"12", "fecha(1i)"=>"2015", "impuesto"=>"18.00", "customer_id"=>"69", "status_id"=>"1", "detail_tickets_attributes"=>{"0"=>{"item"=>"1", "code"=>"12", "description"=>"21321ddsd", "cantidad"=>"12", "price"=>"12", "_destroy"=>"false"}}}, "commit"=>"Crear"} User Load (0.1ms) SELECT `users`.* FROM `users` WHERE `users` (0.1ms) BEGIN cantidad , price: – Croelanjr Dec 20 '15 at 03:07
  • Which page you have posted above in the question? Is it the page which gets rendered after the create action and from where are you getting the `price` and `cantidad` parameters? – Sahil Dec 20 '15 at 04:41
  • I've posted it before creating or updating. – Croelanjr Dec 20 '15 at 15:12
  • **<% @total_price = p.object.cantidad.to_i * p.object.price.to_i %>**. Let me know if it works. I hope [this]( http://stackoverflow.com/a/5224847) answer might help you. – Sahil Dec 20 '15 at 20:31
  • Did it work for you? Because this is first time I came across this through your question. – Sahil Dec 24 '15 at 03:31

0 Answers0