如何使用Rails从numberfield做一个jQuery的ajax调用做一个、如何使用、Rails、numberfield

2023-09-10 18:06:09 作者:浮生思过往

我做了 fixeddeposit 的项目,其中我有三种模式:

I am doing a fixeddeposit project, in which I am having three models:

1) fixeddeposit 2)的InterestRate 3) interestsetup

当我打开一个 fixeddeposit ,我必须键入天数(365/730/1095/1460和放大器; 1825)从 number_field 。然后在另一个文本框中我想更新 rate_of_interest 基于数量(365/730/1095/1460和放大器; 1825),我输入。

When I open a fixeddeposit, I have to type the number of days(365/730/1095/1460 & 1825) from number_field. Then in another text box I want to update the rate_of_interest based upon the number(365/730/1095/1460 & 1825) I typed.

Rate_of_interest应取自率这是在interestrates表字段。请参见下面我interestrates表的截图。

Rate_of_interest should be taken from rate which is in the interestrates table field. Kindly see the screenshot of my interestrates table below.

Interestrates表

例如:

如果我输入fixeddeposit表格编号365(这是number_field)。它会自动从interestrates表中使用AJAX功能选择率为9.5%。

If i type number 365 in fixeddeposit form(which is number_field). It automatically select rate 9.5% from interestrates table using AJAX function.

和,如果客户的年龄超过58,和我输入fixeddeposit表格编号365。它会自动选择(利率9.5%+高级递增0.5%)=从interestrates表中使用AJAX功能的10.0%。

And, if a customer age is above 58, and i type number 365 in fixeddeposit form. It automatically select (rate 9.5% + senior increment 0.5% ) = 10.0% from interestrates table using AJAX function.

FD截图

Screenshot of FD

我GOOGLE了它的解决方案,但是,结果显示具有collection_select孤单。我想在number_field这样做。虽然,我是新来的Ruby on Rails和AJAX我挣扎吧。

I googled it for a solution but, results showing with collection_select alone. I want to do the same in number_field. Though, i am new to Ruby on Rails and AJAX i am struggling with it.

推荐答案

让我们通过AJAX在轨的步骤

Lets go through the steps of ajax in rails

一个。为您的自定义操作的路线,因为我们想将数据发送因此这将是POST请求

a. Create a route for your custom action, since we want to send data so it'll be post request

post "/rate" => "your_controller#calculate_rate" , as: "calculate_rate"

乙。写一个javascript code,它会听你的月经输入字段。

b. Write a javascript code that will listen to your periods input field.

$(document).on("change","#id_of_periods_input",function(){
  var periods = $(this).val();
  var age = $("#id_of_age_input").val();
  $.ajax({
    type: "POST",
    url: "/rate",
    data: { periods: periods, age: age } //this will enable you to use params[:periods] and params[:age] in your controller
  });
});

这会工作,但的我认为,如果你有某种形式的按钮,当用户点击它,然后计算出你的利率是会更好的,因为如果你使用的的更改事件再那么可以说您的周期值是365让你的Ajax调用将火3次(第一次当你输入3,然后6,然后5)的

This will work but i think it'll be better if you have some sort of button and when user clicks on it then calculate your rate of interest because if you'll use on change event then then lets say your periods value is 365 so your ajax call will fire 3 times( first time when you enter 3 then for 6 and then for 5)

如果你想使用按钮,然后还必须使用相同的逻辑只有这条线会改变

If you want to use button then also you have to use same logic only this line will change

$(document).on("click","#id_of_btn",function(){
  // same logic
});

℃。让您的数据,控制器和执行你的逻辑

c. Getting your data in controller and performing your logic

def calculate_rate
  @periods = params[:periods]
  @age = params[:age]
  // perform your logic here and store rates value in lets say @rate
  respond_to do |format|
    format.js{}  #this will make rails look for a file named calculate_rate.js.erb in your views
  end
end

Ð。通过写JS的填充率输入字段的 calculate_rate.js.erb

$("#id_of_rate_field").val(@rate);