2013-09-24 34 views
0

我试图创建一个类型从DOLARS变化对欧元我怎么可以多2个text_field_tags?

事情是这样的:

  • 一个text_field它显示了我dolars的总和。
  • 另一个text_field其中i可以键入一个数字。
  • 另一个text_field其中示出了dolars的总和*数量类型

这是我的结构

______________  _____________  ______________ 
    |____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 -%> 

我真的感谢所有帮助请,有人可以帮我这个好吗?

回答

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 

希望这将帮助你

+0

奥基我会尝试 –

+0

太感谢你了,它的工作为了我。 非常感谢。 –