jQuery ajax调用从一个数字字段使用Rails不工作

jQuery ajax call from a numberfield using Rails is not working

本文关键字:字段 数字 Rails 工作 一个 调用 ajax jQuery      更新时间:2023-09-26

对于我的应用程序,我有定期存款,我们可以在其中创建新的定期存款。现在,我想根据数字(365/730/1095/1460)更新利率字段。我输入了存款期限(number_field),我必须检查客户的年龄。

我已经在fixeddeposits_controller中计算了客户年龄。我不知道我错在哪里,too不工作。

Example 1:
1.1: If a customer age >58 && age<75,  i want to open the fixed deposit for 365days means i have  to sum the two fields rate(9.5%) + seniorincrement(0.5%) and then pass the value(10.0%) to rateofinterest field.
1.2: If a customer age >75,  i want to open the fixed deposit for 365days means i have  to sum the two fields rate(9.5%) + superseniorincrement(1.0%) and then pass the value(10.5%) to rateofinterest field.
1.3: If a customer age <58,  i want to open the fixed deposit for 365days means i have to pass the rate(9.5%) field value alone to rateofinterest field.

其中as(rate, seniorincrement, superseniorincrement)字段来自利息表

为此,我使用AJAX/JQUERY,这是Mandeep在我之前的问题中建议的。

我已经实现了,但它不工作。我已经附上了我试过的代码。请检查一下,并给我一些建议。

_form.html.erb

   <%= form_for @fixeddeposit do |f| %>  
   <% if @fixeddeposit.errors.any? %>
   <h4>Couldn't open FD Account</h4>
   <ul>
   <% @fixeddeposit.errors.full_messages.each do |error| %>
   <li><%= error %></li>
   <% end %>
   </ul>
  <% end %>
  <%= f.label :customer_name, class:'required' %>
  <%= f.text_field :customername, :placeholder =>'Name' %>
  <%= f.label :date_of_birth, class:'required' %>
  <%= f.date_select :dateofbirth, { :include_blank => true, :start_year => 1900, :end_year => 2014 }, :id => "dateofbirth" %>

  <%= f.label :Periods, class:'required' %>
  <%= f.number_field :periods, :id => "fixeddeposit_periods", :placeholder => "Days", :class => "input-mini" %>
  <%= f.label :Rate_Of_Interest %>
  <%= f.text_field :rateofinterest, :id => "fixeddeposit_rateofinterest", :value => "", :disabled => true, :class => "input-medium" %>
  <span class="help-block">auto-generated</span>
  <div>      
  </div>
  <%= f.submit "Open FD", class: "btn btn-primary" %>     
  <% end %>
  </div>
  </div>

application.js

$(document).on("change","#fixeddeposit_periods",function(){
var periods = $(this).val();
var dateofbirth = $("#dateofbirth").val();
$.ajax({
type: "POST",
url: "/rateofinterest",
data: { periods: periods, dateofbirth: dateofbirth }
});
});

fixeddeposits_controller

  def calculate_age(dateofbirth)
    @age = DateTime.now - dateofbirth/ 365
  end
  def calculate_rateofinterest
    @periods = params[:periods]
    @dateofbirth = params[:dateofbirth]
    calculate_age(@dateofbirth)
    if @age >= 58 && @age < 75
      @rateofinterest = Rateofinterest.select('interestrates.id, interestrates.seniorincrement')
    elsif @age >= 75
      @rateofinterest = Rateofinterest.select('interestrates.id, interestrates.superseniorincrement') 
    else
      @rateofinterest = Rateofinterest.select('interestrates.id, interestrates.rate')
    end
   respond_to do |format|
   format.html{ redirect_to fixeddeposits_path }    
   format.js{}
   format.json{}
   end
  end

calculate_rateofinterest.js.erb

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

routes.rb

resources :fixeddeposits do
resources :interestrates
end
post "/rateofinterest" => "fixeddeposits#calculate_rateofinterest" , as: "calculate_rateofinterest"

我不知道为什么它不工作。

首先,在js代码中替换

var dateofbirth = $("#fixeddeposit_dateofbirth").val();

// you have mentioned your dob field's id as 'dateofbirth'    
var dateofbirth = $("#dateofbirth").val(); 

在您的控制器中,您需要调用calculate_age方法来获得@age变量。替换

@dateofbirth = params[:dateofbirth]

@dateofbirth = params[:dateofbirth]
calculate_age(@dateofbirth)

我不确定,为什么你在你的calculate_age方法定义中写了@age.save。你可以把它取下来。


现在在calculate_rateofinterest.js.erb文件中替换

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

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