2014-07-21 1 views
0

제 신청서에는 새로운 fixeddeposits를 작성할 수있는 fixeddeposits가 있습니다. 이제, number (365/730/1095/1460 & 1825)에 기초하여 rateofinterest 필드를 업데이트하려고합니다. 입금 기한 (number_field)을 입력 했으므로 고객 나이를 확인해야합니다.Rails를 사용하여 숫자 필드에서 jQuery ajax 호출이 작동하지 않습니다.

나는 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. 

여기서 (비율, 우선 순위, 상위 오름차순) 필드는 interestrates 테이블입니다.

이건 내가 전에 question에서 Mandeep이 제안한 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> 

$(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 application.js

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 

그것이 작동하지 않는 이유를 모르겠어요

resources :fixeddeposits do 
resources :interestrates 
end 

post "/rateofinterest" => "fixeddeposits#calculate_rateofinterest" , as: "calculate_rateofinterest" 

calculate_rateofinterest.js.erb

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

routes.rb. 이 문제를 해결할 수 있도록 도와주세요. 모든

답변

1

먼저, JS 코드 컨트롤러에서

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

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

교체, 당신은 @age 변수를 가지고 calculate_age 메소드를 호출 할 필요가있다. 당신이 당신의 calculate_age 방법 정의에 @age.save을 쓴 이유를 잘 모르겠어요

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

@dateofbirth = params[:dateofbirth] 

교체합니다. 제거 할 수 있습니다.


는 이제 calculate_rateofinterest.js.erb 파일에
$("#fixeddeposit_rateofinterest").val(@rate); 

그것은 당신을 도울 것입니다 희망과

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

를 교체합니다.

+0

안녕하세요. 회신 해 주셔서 감사합니다. 내가 제안한 코드를 편집했지만 너무 효과적이지 않습니다. . . : – Yogesh

+0

당신은 당신이 오류를 보여줄 필요가있다. – RAJ

+0

레일스와 아약스에서 루비에 대해 아주 익숙해 져서 AJAX 오류를 어떻게 볼 수 있을지 모르겠다. – Yogesh

관련 문제