我正在尝试使用 Savon gem 将货币转换器集成到我的 Ruby on Rails 应用程序中,以从http://www.webservicex.net/CurrencyConvertor.asmx?WSDL获取数据。我正在学习http://www.mindfiresolutions.com/How-to-use-Savon-in-Ruby-on-Rails-Application-2367.php上的教程,但是当我单击提交按钮。我完全按照教程所说的进行了操作。这是我的代码。
我有一个控制器,
class ConverterController < ApplicationController
def index
end
def show
end
def create
# creating a object of the CurrencyConverter model
currency = CurrencyConverter.new(params[:fromCurrency],params[:toCurrency])
render :json => currency.result
end
end
模型:
class CurrencyConverter < ActiveRecord::Base
require 'savon'
attr_reader :result
# purpose : for initializing the object of currency converter
# params : fromCurrecny(unit),toCurrency(unit)
# return : none
def initialize(fromCurrency , toCurrency)
# creating a client from the wsdl schema
client = Savon::Client.new("http://webservicex.net/currencyconvertor.asmx?wsdl")
# calling the api with fromCurrecny and toCurrency unit
response = client.request :web, :conversion_rate, body: {
"FromCurrency" => fromCurrency , "ToCurrency" => toCurrency
}
#checking for success of api call
if response.success?
data = response.to_array(:conversion_rate_response).first
if data
# setting convertion rate to result
@result = data[:conversion_rate_result]
end
end
end
end
我在 index.html.erb 中的看法
<h1>Public#index</h1>
<p>Find me in app/views/public/index.html.erb</p>
<div id="welcome">
<label> From Currency Unit</label>
<select name="from_currency" id="from_currency">
<option value="">Select Currency</option>
<option value="GBP">British Pound</option>
<option value="INR">Indian Rupee</option>
<option value="PKR">Pakistani Rupee</option>
<option value="SGD">Singapore Dollar</option>
<option value="ZAR">South African Rand</option>
<option value="USD">U.S. Dollar</option>
</select>
<input type="text" id="from_amount" placeholder="Enter From Curreny Amount"/>
<br>
<label> To Currency Unit</label>
<select name="to_currency" id="to_currency">
<option value="">Select Currency</option>
<option value="GBP">British Pound</option>
<option value="INR">Indian Rupee</option>
<option value="PKR">Pakistani Rupee</option>
<option value="SGD">Singapore Dollar</option>
<option value="ZAR">South African Rand</option>
<option value="USD">U.S. Dollar</option>
</select>
<input type="text" readonly id="to_amount" placeholder="to Curreny Amount"/>
<br>
<input type="button" class="btn" id="get_conversion_rate" value="Get Conversion">
我正在使用 ajax 调用来提交这个 fromCurrency 和 toCurrency 单位。我将代码放在 /assets/javascripts/CurrencyConverter.js 下,然后在 application.html.erb 文件中添加了 <% javascript_include_tag :all %> 行以包含它们它的代码是
$(document).ready(function(){
var rate = 0.0;
var fromCurrency = $("#from_currency");
var toCurrency= $("#to_currency");
var fromAmount = $("#from_amount");
var toAmount = $("#to_amount");
var button = $('#get_conversion_rate');
var getConversionRate = function(){
$.post('/publics',{
fromCurrency : fromCurrency.val(),
toCurrency : toCurrency.val()
},function(data) {
rate = data;
toAmount.val(fromAmount.val()*rate);
});
};
var initializer = function(){
button.click(getConversionRate);
};
initializer();
});
请帮忙。