开发者

jQuery Ajax in Rails 3

开发者 https://www.devze.com 2023-03-31 03:03 出处:网络
I am getting a status of 404 Not Found when trying to submit an Ajax request to one of my controllers.

I am getting a status of 404 Not Found when trying to submit an Ajax request to one of my controllers.

Here is my 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" %>
  <%= 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>

And here is my controller:

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

I have been Googling for hours on this one. Any help will be greatly appreciated.


In your routing, you need to ensure that calculateCost (which, by the way, should be calculate_cost in the ruby world) is a collections route not a member route because you are not specifying the id.

0

精彩评论

暂无评论...
验证码 换一张
取 消