2011-08-26 2 views
2

여러 광고 항목을 지원하는 견적 양식을 작성하려고합니다. 다음 필드에 작성하여 견적 라인 항목을 완료 한 후 :Rails 3에서 jQuery Ajax로 데이터베이스 쿼리

  • 속성
  • 높이
  • letter_quantity

나는 곱하는 단가를 얻을 수있는 데이터베이스 테이블을 조회하고 싶습니다 letter_quantity를 선택하고 광고 항목의 비용을 표시합니다. 내 생각은 쿼리를 실행하고 letter_quantity 필드에서 광고 항목의 비용 onBlur를 표시하는 것입니다.

여기 내 양식이다

<%= nested_form_for @estimate do |f| %> 

    <%= f.label :date %><br /> 
    <%= f.text_field :date %><br /> 
    <%= f.label :job_name %><br /> 
    <%= f.text_field :job_name %><br /> 

    <%= f.fields_for :items do |builder| %> 
     <%= builder.label :properties %><br /> 
     <%= builder.text_field :properties %><br /> 
     <%= builder.label :height %><br /> 
     <%= builder.text_field :height %><br /> 
     <%= builder.label :letter_quantity %><br /> 
     <%= builder.text_field :letter_quantity %><br /> 
     <%= builder.link_to_remove "Remove this item" %> 
    <% end %> 
    <%= f.link_to_add "Add a item", :items %> 
<%= f.submit "Add item" %> 

<% end %> 

내가 쿼리를 실행하고 다시 형태로 비용을 반환하는 함수를 호출하는 일반적인 jQuery를 아약스 기능을 사용하고 싶습니다.

$.ajax({ 
    url: "test.html", 
    context: document.body, 
    success: function(){ 
    $(this).addClass("done"); 
    } 
}); 

class ItemsController < ApplicationController 

    def calculateCost 
     #Execute SQL query here 
      SELECT unit_price 
      FROM CostData 
      WHERE properties = form.properties 
      AND height = form.height 

     #Return unit_price x form.letter_quantity to form 
    end 

end 

, 가장 중요한 것은 어떻게 설정 calculateCost 함수를 호출 할 수있는 jQuery를 아약스 기능을 수행 여기에 기본 템플릿은 무엇입니까? Google을 검색해 본 결과이 예제를 찾을 수 없습니다.

2 차적으로, calculateCost 함수에서 쿼리와 반환 기능을 어떻게 설정합니까? 이 질문에 대한 도움은 매우 감사하겠습니다. 컨트롤러에서

<%= javascript_include_tag "jquery-1.6.2.min.js", "jquery.form" %> 


<%= nested_form_for @estimate do |f| %> 

    <%= f.label :date %><br /> 
    <%= f.text_field :date %><br /> 
    <%= f.label :job_name %><br /> 
    <%= f.text_field :job_name %><br /> 

    <%= f.fields_for :items do |builder| %> 
     <%= builder.label :properties %><br /> 
     <%= builder.text_field :properties, :id => "properties_field" %><br /> 
     <%= builder.label :height %><br /> 
     <%= builder.text_field :height, :id => "height_field" %><br /> 
     <%= builder.label :letter_quantity %><br /> 
     <%= builder.text_field :letter_quantity, :id => "letter_quantity_field" %><br /> 
     <%= builder.link_to_remove "Remove this item" %> 
    <% end %> 
    <%= f.link_to_add "Add a item", :items %> 
    <%= f.submit "Add item" %> 

<% end %> 

<script type="text/javascript"> 
    $("#letter_quantity_field").onBlur(function() { 
     var height = $("#height_field").val(); 
     var properties = $("#properties_field").val(); 
     $.ajax({ 
      url: '/items/calculateCost?height=' + height + '&properties=' + properties , type: 'get', dataType: 'html', 
      processData: false, 
      success: function(data) { 
       if (data == "record_not_found") { 
        alert("Record not found"); 
       } 
       else { 
        $("#letter_quantity_field").val(data); 
       } 
      } 
     }); 
    }); 
</script> 

가 그런 짓을 :

답변

3

페이지의 상단에 JQuery와 및 jquery.form 플러그인을 포함한다.

def calculateCost 
    @cost_data = CostData.find_by_properties_and_find_by_height(params[:properties], params[:height]) 
    if @cost_data.blank? 
     render :text => "record_not_found" 
    else 
     render :text => @cost_data.unit_price 
    end 
    end 
+0

이것은 작동하지 않습니다. 내가 지정한 ajax 함수를 설정했지만 Google Dev Tools에서 요청을 볼 때 404 Not Found 상태/응답을 얻고 있습니다. –

+1

calculateCost 액션을 매핑하기 위해 Collection 라우트를 추가했습니다. 이제 작동합니다. 당신의 도움을 주셔서 감사합니다! –

관련 문제