2011-08-28 99 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

这个伎俩!非常感谢您的帮助。 –