表格提交两次:remote =>真正的Rails 3.2

时间:2012-04-04 14:32:20

标签: forms ruby-on-rails-3.2

我有以下表格:

= form_for([current_user,@company], :remote => true) do |f|
  -if @company.errors.any?
    #error_explanation
      %h2= "#{pluralize(@company.errors.count, "error")} prohibited this company from being saved:"
      %ul
        - @company.errors.full_messages.each do |msg|
          %li= msg
  =f.label :name
  =f.text_field :name
  =f.label :address
  =f.text_area :address, :rows => 3, :cols => 5
  =f.label :phone_number
  =f.text_field :phone_number
  .actions
    = f.submit 'Save'

当我点击保存按钮时,我可以在服务器日志中看到以下内容:

Started POST "/users/1/companies" for 127.0.0.1 at 2012-04-04 21:27:50 +0700
Processing by CompaniesController#create as JS
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"6ZH9hAUuf5ZTCf8Loc4M/IIl/Etzm7uDGoYbIgCTvhI=", "company"=>{"name"=>"test", "address"=>"test", "phone_number"=>"5454543"}, "commit"=>"Save", "user_id"=>"1"}
   (0.2ms)  BEGIN
  SQL (25.2ms)  INSERT INTO "companies" ("address", "name", "phone_number", "url", "user_id") VALUES ($1, $2, $3, $4, $5) RETURNING "id"  [["address", "test"], ["name", "test"], ["phone_number", "5454543"], ["url", nil], ["user_id", nil]]
   (1.6ms)  COMMIT
  Rendered companies/create.js.erb (0.7ms)
Completed 200 OK in 41ms (Views: 8.1ms | ActiveRecord: 27.0ms)


Started POST "/users/1/companies" for 127.0.0.1 at 2012-04-04 21:27:50 +0700
Processing by CompaniesController#create as JS
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"6ZH9hAUuf5ZTCf8Loc4M/IIl/Etzm7uDGoYbIgCTvhI=", "company"=>{"name"=>"test", "address"=>"test", "phone_number"=>"5454543"}, "commit"=>"Save", "user_id"=>"1"}
   (0.7ms)  BEGIN
  SQL (2.7ms)  INSERT INTO "companies" ("address", "name", "phone_number", "url", "user_id") VALUES ($1, $2, $3, $4, $5) RETURNING "id"  [["address", "test"], ["name", "test"], ["phone_number", "5454543"], ["url", nil], ["user_id", nil]]
   (12.8ms)  COMMIT
  Rendered companies/create.js.erb (0.1ms)
Completed 200 OK in 30ms (Views: 10.5ms | ActiveRecord: 16.1ms)

这意味着表单正在提交两次。 我从我的公共目录中删除了assets文件夹。我还检查了页面上呈现的html,并且没有包含jquery或其他依赖项的双重内容。

为什么要提交两次?

2 个答案:

答案 0 :(得分:4)

我运行了rake资产:预编译并在开发模式下运行我的服务器。解决方案是耙资产:清理并重新启动服务器

答案 1 :(得分:1)

我的解决方法:

 rake assets:precompile
 with //= require jquery_ujs in your app/assets/javascripts/application.js

在rake之后,删除行from app/assets/javascripts/application.js并启动服务器。 现在它应该按预期工作。