rails 3: jquery ajax form calling twice?
I am seeing quite a weird behavior on a simple form:
Here's the form:
<%= form_for @product_list, :url => add_product_admin_product_list_path(@product_list, :product_id => @product), :html => {'data-remote' => true} do |f| %>
<p class="<%= cycle('odd', 'even') %>"><%= @product.short_description %></p>
<%= f.submit "Add Item!" unless @product_list.products.include?(@product) %>
<% end %>
On the server I am seeing:
Started POST "/admin/product_lists/featured-product-list-for-guests/add_product?product_id=fisher-price-rock-a-stack" for 127.0.0.1 at Fri Nov 12 15:50:59 +0100 2010
Processing by Admin::ProductListsController#add_product as JS
Parameters: {"product_id"=>"fisher-price-rock-a-stack", "authenticity_token"=>"/p5O3g0eY/zgCTq0yL9wG5XZicHrGo/FAXvlNzfoa+s=", "utf8"=>"✓", "id"=>"featured-product-list-for-guests"}
Completed 200 OK in 154ms (Views: 25.6ms开发者_运维知识库 | ActiveRecord: 2.5ms)
Started POST "/admin/product_lists/featured-product-list-for-guests/add_product?product_id=fisher-price-rock-a-stack" for 127.0.0.1 at Fri Nov 12 15:50:59 +0100 2010
Processing by Admin::ProductListsController#add_product as JS
Parameters: {"product_id"=>"fisher-price-rock-a-stack", "authenticity_token"=>"/p5O3g0eY/zgCTq0yL9wG5XZicHrGo/FAXvlNzfoa+s=", "utf8"=>"✓", "id"=>"featured-product-list-for-guests"}
Completed 200 OK in 125ms (Views: 24.1ms | ActiveRecord: 1.4ms)
Basically it is calling this twice!
Also, in my routes I have this:
namespace :admin do
resources :product_lists do
member do
put :add_product
put :remove_product
end
end
end
I am a bit surprised it has to be a put (a post doesn't work) but maybe that is another question!
Nevermind! It turns out that I was including jquery twice...
FYI if you change this line in application.rb:
config.action_view.javascript_expansions[:defaults] = %w(jquery rails application)
You don't need to also include anything new for jquery with a javascript_include_tag
精彩评论