2011-08-28 78 views
0

嘗試向我的控制器之一提交Ajax請求時,狀態爲404 Not Found。Rails 3中的jQuery Ajax

這裏是我的形式:

<%= 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" %> 
    <%= 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").blur(function() { 
     var height = $("height_field").val(); 
     var properties = $("properties_field").val(); 
     $.ajax({ 
      url: '/estimates/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> 

這裏是我的控制器:

class EstimatesController < ApplicationController 
    . 
    . 
    . 
    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 

end 

我一直在谷歌搜索在這一個小時。任何幫助將不勝感激。

+0

你是如何指定你的路線? – Pravin

回答

0

在你的路由中,你需要確保calculateCost(順便說一句,在ruby世界中應該是calculate_cost)是一個不是成員路由的集合路由,因爲你沒有指定id。

+0

這個伎倆!非常感謝您的幫助。 –