![](/img/trans.png)
[英]Ruby On Rails: How do I create multple text_field_tags that store the values into the same array?
[英]How can i multiple 2 text_field_tags?
我正在嘗試創建一種從DOLARS到Euros的兌換類型
像這樣的東西:
這是我的結構
______________ _____________ ______________
|____value____| * [sum_of_dolars] = [dolar_to_euro]
這是我的三列桌子
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 -%>
我真的很感謝所有幫助,有人可以幫助我嗎?
您需要在視圖中使用像這樣的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
希望這個能對您有所幫助
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.