我正在尝试创建一种从DOLARS到Euros的变更
这样的事情:
这是我的结构
______________ _____________ ______________
|____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 :(得分: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
希望这会对你有所帮助