错误:提交表单正在创建两个对象

时间:2012-09-16 00:00:35

标签: ruby-on-rails ruby

我正在尝试创建包含所有现有维护的索引页面。 一个链接,当您单击它时,会显示一个用于创建新建立的表单。

Controller:
  def index
    @stablishments = Stablishment.all
    @new_stablishment = Stablishment.new()
  end

  def create
    @stablishment = Stablishment.new
    @stablishment.update_attributes(params[:stablishment])
  end


Index:
Index

<div id="create_stablishment">
  <%= link_to "Crear Nuevo Establecimiento", '#' %>
</div>

<div id="stablishment_form_fields" style="display:none;">
  <%= form_for(@new_stablishment, :remote => true) do |f| %>
    <%= render :partial => 'stablishments/form_fields', :locals => {f: f} %>
  <% end %>
</div>

<div id="stablishments_list">
  <% @stablishments.each_with_index do |stablishment, i| %>
    <%= render stablishment, :count => i %>
  <% end %>
</div>

<div id="mensaje"></div>

form_fields:
<div class="form_fields">
  <%= f.label "Nombre" %>
  <%= f.text_field :name %>
</div>
<div class="form_fields">
  <%= f.label "Ciudad" %>
  <%= f.text_field :city %>
</div>
<div class="form_fields">
  <%= f.label "Estado" %>
  <%= f.text_field :state %>
</div>
<div class="form_fields">
  <%= f.label "Direccion" %>
  <%= f.text_field :address %>
</div>
<div class="form_fields">
  <%= f.label "Email" %>
  <%= f.text_field :mail %>
</div>
<div class="form_fields">
  <%= f.label "Sitio Web" %>
  <%= f.text_field :web_site %>
</div>
<div class="form_fields">
  <%= f.label "Tipo" %>
  <%= f.text_field :stablishment_type %>
</div>
<div class="form_fields">
  <%= f.submit %>
</div>
create.js:
$("#stablishment_form_fields").hide();
$("#mensaje").html("se ha creado");

当我点击创建stablishments时,它正在创建对象。日志:

Started POST "/stablishments" for 127.0.0.1 at 2012-09-15 20:47:05 -0300
Processing by StablishmentsController#create as JS
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"VDf47PuVfNH7UPLEyVqtY4n+2DgmCKT3Jvx2wljMtZY=", "stablishment"=>{"name"=>"6", "city"=>"", "state"=>"", "address"=>"", "mail"=>"", "web_site"=>"", "stablishment_type"=>""}, "commit"=>"Create Stablishment"}
   (0.2ms)  BEGIN
  SQL (0.4ms)  INSERT INTO `stablishments` (`address`, `city`, `created_at`, `mail`, `name`, `phone`, `stablishment_type`, `state`, `updated_at`, `web_site`) VALUES ('', NULL, '2012-09-15 23:47:05', '', '6', NULL, NULL, NULL, '2012-09-15 23:47:05', '')
   (119.6ms)  COMMIT
  Rendered stablishments/create.js.erb (0.8ms)
Completed 200 OK in 176ms (Views: 7.4ms | ActiveRecord: 120.2ms)


Started POST "/stablishments" for 127.0.0.1 at 2012-09-15 20:47:05 -0300
Processing by StablishmentsController#create as JS
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"VDf47PuVfNH7UPLEyVqtY4n+2DgmCKT3Jvx2wljMtZY=", "stablishment"=>{"name"=>"6", "city"=>"", "state"=>"", "address"=>"", "mail"=>"", "web_site"=>"", "stablishment_type"=>""}, "commit"=>"Create Stablishment"}
   (1.0ms)  BEGIN
  SQL (0.4ms)  INSERT INTO `stablishments` (`address`, `city`, `created_at`, `mail`, `name`, `phone`, `stablishment_type`, `state`, `updated_at`, `web_site`) VALUES ('', NULL, '2012-09-15 23:47:05', '', '6', NULL, NULL, NULL, '2012-09-15 23:47:05', '')
   (58.9ms)  COMMIT
  Rendered stablishments/create.js.erb (0.1ms)
Completed 200 OK in 76ms (Views: 10.3ms | ActiveRecord: 60.3ms)

为什么会发生这种情况?

2 个答案:

答案 0 :(得分:0)

不要实例化@stablishment然后在其上调用update_attributes,而只需使用:

def create
  @stablishment = Stablishment.new(params[:stablishment]).save
end

答案 1 :(得分:0)

似乎有两个请求被发送。问题可能来自Javascript,因为您使用的是远程表单。

form_for(@new_stablishment, :remote => true)

将自动设置和调用AJAX帖子并由rails.js或jquery_ujs.js调用。所以我们不需要手动处理它。

因此,要解决此问题,您可以在浏览器中显示控制台日志(我想在Firefox中使用Firebug)。如果有两个请求,则表示您的javascript代码中存在错误。