0

DOLARS から Euros への種類の変更を作成しようとしています

このようなもの :

  • ドルの合計額を表示する text_field。
  • 数値を入力できる別の text_field。
  • ドルの合計額 * 入力した数値を表示する別の text_field

これが私の構造です

  ______________        _____________       ______________
  |____value____|  *   [sum_of_dolars]  =  [dolar_to_euro]

これは3列の私のテーブルです

 Policies
   |id|   |mount|   |type_money|  .....all my columns are Integer
     1      100        1
     2      120        1
     3       80        1
     4      120        1

これは私のコントローラーです

class PolicyManagement::PolicyController < ApplicationController

  def calculator
    @policies = Policy.find(:all)
    @dolar= Policy.sum(:mount, :conditions=>["type_money = '1' "])
    @dolar_to_euro= @dolar * @type_of_change
  end

end

これは私のモデルです

class Policy < ActiveRecord::Base
     #nothing
end

これが私の見解です

<%= form_tag('/calculator') do -%> 
  <% text_field_tag "dolars",@dolar %>
  <% text_field_Tag "type_of_change", @change %>
  <% text_field_tag "dolar_to_euro",@dolar_to_euro %>
  <% submit_tag "Results" %> 
<% end -%> 

私は本当にすべての助けをお願いします、誰かが私を助けてくれますか?

4

1 に答える 1

0

ビューでこのようなJavaScriptを使用する必要があります

    <script type="text/javascript">
        function doMath()
        {
          // Capture the entered values of two input boxes
          var euro = document.getElementById('euro').value;

          var euro = Math.floor(parseFloat(euro) * 100) / 100;
          document.getElementById('euro').value = euro.toFixed(2);

          var cost = document.getElementById('cost').value;
          var dolar =document.getElementById('dolar').value;

          // Add them together and display
          var subtotal = Math.floor(parseFloat(euro) * 100) / 100 * Math.floor(parseFloat(costo) * 100) / 100;
          document.getElementById('subtotal').value = subtotal.toFixed(2);

          var total = parseFloat(subtotal) + parseFloat(dolar);
          document.getElementById('total').value = total.toFixed(2);
        }
    </script>

   <%= text_field_tag "euro", @euro  %>
   <%= text_field_tag "dolar", @dolar  %>
   <%= text_field_tag "cost", :onchange=>"doMath();"  %>
   <%= text_field_tag "subtotal", params[:subtotal] %>
   <%= text_field_tag "total", params[:total]  %>

あなたのコントローラー

 def calculator
   @policies = Policy.find(:all)
   @dolar= Policy.sum(:mount, :conditions=>["type_money = '1' "])
   @euro=  Policy.sum(:mount, :conditions=>["type_money = '2' "])
 end

これがあなたを助けることを願っています

于 2013-11-07T15:41:31.177 に答える