How to update field through Ajax in Ruby on Rails form?
In my form I am trying to update the exchange_rate field when the user
changes the currency select box.
application.js:
$("#invoice_currency").change(function() {
$.ajax({
url: '/invoices/get_exchange_rate',
dataType: 'script'
})
});
invoices_controller.rb:
def get_exchange_rate
from = current_user.base_currency
to = params[:currency]
@exchange_rate = GoogleCurrency.get_exchange_rate(from, to)
end
get_exchange_rate.js.erb:
$('#invoice_exchange_rate').val('<%= @exchange_rate %>');
google_currency.rb:
module GoogleCurrency
def self.get_exchange_rate(from, to)
....
end
end
This isn't working yet because, for some reason, params[:currency] can't
be evaluated dynamically through Ajax.
Can anybody tell me how it's done?
Thanks for any help.
No comments:
Post a Comment