rails 3:jquery ajax表单调用两次?

时间:2010-11-12 14:57:09

标签: jquery ruby-on-rails ajax

我在一个简单的表格上看到了相当奇怪的行为:

以下是表格:

<%= 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 %>

在我看到的服务器上:

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)

基本上它是两次调用它!

另外,在我的路线中,我有这个:

  namespace :admin do
    resources :product_lists do
      member do
        put :add_product
        put :remove_product
      end
    end
  end

我有点惊讶它必须是一个put(帖子不起作用)但也许这是另一个问题!

1 个答案:

答案 0 :(得分:4)

没关系!事实证明我曾两次包括jquery ......

如果您在application.rb中更改此行,那么仅供参考:

config.action_view.javascript_expansions[:defaults] = %w(jquery rails application)

您不需要使用javascript_include_tag

为jquery添加任何新内容