Javascript 使用Rails从numberfield调用jQuery ajax无效
对于我的应用程序,我有一个FixedDepositos,我们可以在其中创建新的FixedDepositos。现在,我想根据我在存款期(number_字段)中键入的号码(365/730/1095/1460和1825)更新rateofinterest字段,并且我必须检查客户年龄 我在fixeddeposits\u controller中计算了客户年龄。我不知道我错在哪里,这也不起作用Javascript 使用Rails从numberfield调用jQuery ajax无效,javascript,jquery,ruby-on-rails,ajax,ruby-on-rails-3,Javascript,Jquery,Ruby On Rails,Ajax,Ruby On Rails 3,对于我的应用程序,我有一个FixedDepositos,我们可以在其中创建新的FixedDepositos。现在,我想根据我在存款期(number_字段)中键入的号码(365/730/1095/1460和1825)更新rateofinterest字段,并且我必须检查客户年龄 我在fixeddeposits\u controller中计算了客户年龄。我不知道我错在哪里,这也不起作用 Example 1: 1.1: If a customer age >58 && age&l
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.
固定存储\u控制器
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
routes.rb
<%= 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>
$("#fixeddeposit_rateofinterest").val(@rate);
resources :fixeddeposits do
resources :interestrates
end
post "/rateofinterest" => "fixeddeposits#calculate_rateofinterest" , as: "calculate_rateofinterest"
我不知道为什么它不起作用。请帮助我解决此问题。首先,在您的js代码中替换
var dateofbirth = $("#fixeddeposit_dateofbirth").val();
与
在控制器中,您需要调用
calculate\u age
方法来设置@age
变量。替换
@dateofbirth = params[:dateofbirth]
与
我不确定,为什么要在
计算年龄
方法定义中编写@age.save
。您可以删除它
现在,在您的
计算\u rateofinterest.js.erb
文件中,替换
$("#interestrates_rate").val(@rate);
与
希望对您有所帮助。您好,谢谢您的回复。我已编辑了您建议的代码,但这也不起作用…:(我对RUBY ON RAILS和AJAX非常陌生,所以我不知道如何看到AJAX错误。您可以检查您的RAILS服务器控制台,或者如果它在客户端,您可以在firebug(firefox)中检查它,或者只需在chrome中打开您的站点,然后按F12键,在控制台选项卡中,您可以通过按照相同的步骤重新生成问题来查看错误我遇到的这两个错误。)(Uncaught ReferenceError:$未定义应用程序。js:1未能加载资源:服务器响应状态为404(未找到)),您需要检查rails服务器控制台以了解404的原因,对于$,您需要在application.js中包含jquery文件
@dateofbirth = params[:dateofbirth]
@dateofbirth = params[:dateofbirth]
calculate_age(@dateofbirth)
$("#interestrates_rate").val(@rate);
$("#fixeddeposit_rateofinterest").val(@rate);