【问题标题】:jQuery ajax call from a numberfield using Rails is not working使用 Rails 从数字字段调用 jQuery ajax 不起作用
【发布时间】: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


    【解决方案1】:

    首先,在你的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);
    

    希望对你有所帮助。

    【讨论】:

    • 您好,感谢您的回复。我已经编辑了您建议的代码,但这也不起作用。 . . :(
    • 我对 RUBY ON RAILS 和 AJAX 非常陌生,所以我不知道如何查看 AJAX 错误。
    • 您可以检查您的 Rails 服务器控制台,或者如果它在客户端,您可以在 firebug (firefox) 中检查它,或者只需在 chrome 中打开您的站点并按 F12,在控制台选项卡中,您可以看到错误通过按照相同的步骤重新生成问题
    • 这两个错误我得到(未捕获的 ReferenceError: $ is not defined application.js:1 Failed to load resource: the server response with a status of 404 (Not Found) localhost:3000/assets/defaults.js)
    • 您需要检查您的 Rails 服务器控制台以了解 404 的原因,对于 $,您需要在 application.js 中包含 jquery 文件
    猜你喜欢
    • 1970-01-01
    • 2012-04-16
    • 2013-10-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-24
    • 1970-01-01
    • 2015-09-05
    相关资源
    最近更新 更多