0

我有以下表格:

= 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 或其他依赖项。

为什么要提交两次?

4

2 回答 2

4

我已经运行了 rake assets:precompile 并在开发模式下运行我的服务器。解决方案是 rake assets:clean 并重新启动服务器

于 2012-04-11T03:22:12.013 回答
1

我的解决方法:

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

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

于 2012-07-25T14:44:36.313 回答