【发布时间】:2014-07-21 09:05:52
【问题描述】:
对于我的应用程序,我有固定存款,我们可以在其中创建新的固定存款。现在,我想根据我在存款期(number_field)中输入的数字(365/730/1095/1460 和 1825)更新利率字段,我必须检查客户年龄。
我在 fixeddeposits_controller 中计算了客户年龄。我不知道我错在哪里,这也不起作用。
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) 字段来自利率表。
为此,我正在使用 Mandeep 在我之前的 question 中建议的 AJAX/JQUERY。
我已经实现了,但它不起作用。我附上了我试过的代码。请检查一下,请给我一些想法。
_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"
我不知道为什么它不起作用。帮我解决这个问题。
【问题讨论】:
标签: javascript jquery ruby-on-rails ajax ruby-on-rails-3