【问题标题】:Rails program duplicate rows on databaseRails程序在数据库上重复行
【发布时间】:2015-05-18 11:28:07
【问题描述】:

我在使用与数据库 postgreSQL 9.1 连接的 ruby​​ on rails 应用程序时遇到问题。

当我点击保存信息时,我有一个应用程序,这会复制数据库中的行。

我在我的程序中搜索任何重复的代码,但我什么也看不到。

有人可以帮我吗?

视图部分:

 <fieldset>
        <legend>Opciones Generales</legend>
        <dl>
            <dt><%= l.label :name %></dt>
            <dd><%= l.text_field :name %></dd>
            <dt><%= l.label :highlight %></dt>
            <dd><%= l.text_area :highlight, :class => 'editor' %></dd>
            <dt><%= l.label :content %></dt>
            <dd><%= l.text_area :content, :class => 'editor' %></dd>
        </dl>
     </fieldset>
      <% end %>
     <div class="actions">
        <%= f.submit "#{smart_label(request.fullpath)}", :class => 'btn green' %>
     </div>

控制器部分:

def create
post = Post.new(params[:post])
  if post.save
    flash[:notice] = "El post se ha creado correctamente."
    @page = post.widget.page
  end
  @object = post
  render 'widgets/create'


end

我认为在模型中没有什么有趣的,但如果你需要我可以粘贴。

插入日志:

Started POST "/backend/posts" for ***.***.***.*** at 2015-05-04 11:37:40 +0200
Processing by PostsController#create as JS
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"****=", "post"=>{"widget_id"=>"8", "homepage"=>"1", "active"=>"1", "translations_attributes"=>{"1"=>{"id"=>"", "locale"=>"ca", "name"=>"prueba", "highlight"=>"<p>hola<br></p>", "content"=>"<p>hola<br></p>"}, "2"=>{"id"=>"", "locale"=>"es", "name"=>"", "highlight"=>"", "content"=>""}}}, "commit"=>"Ejecutar"}
  [1m[36mCustomer Load (1.0ms)[0m  [1mSELECT "customers".* FROM "customers" WHERE "customers"."id" = $1 LIMIT 1[0m  [["id", 3]]
  [1m[35mLang Load (0.8ms)[0m  SELECT "langs".* FROM "langs" WHERE "langs"."customer_id" = 3 AND "langs"."default" = 't' LIMIT 1
  [1m[36m (0.5ms)[0m  [1mBEGIN[0m
  [1m[35mSQL (1.9ms)[0m  INSERT INTO "posts" ("active", "created_at", "header", "homepage", "updated_at", "widget_id") VALUES ($1, $2, $3, $4, $5, $6) RETURNING "id"  [["active", true], ["created_at", Mon, 04 May 2015 09:37:40 UTC +00:00], ["header", nil], ["homepage", true], ["updated_at", Mon, 04 May 2015 09:37:40 UTC +00:00], ["widget_id", 8]]
  [1m[36mSQL (1.6ms)[0m  [1mINSERT INTO "post_translations" ("content", "created_at", "highlight", "locale", "meta_description", "meta_keywords", "name", "post_id", "slug", "title", "updated_at") VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11) RETURNING "id"[0m  [["content", "<p>hola<br></p>"], ["created_at", Mon, 04 May 2015 09:37:40 UTC +00:00], ["highlight", "<p>hola<br></p>"], ["locale", "ca"], ["meta_description", nil], ["meta_keywords", nil], ["name", "prueba"], ["post_id", 286], ["slug", "prueba"], ["title", nil], ["updated_at", Mon, 04 May 2015 09:37:40 UTC +00:00]]
  [1m[35mSQL (0.8ms)[0m  INSERT INTO "post_translations" ("content", "created_at", "highlight", "locale", "meta_description", "meta_keywords", "name", "post_id", "slug", "title", "updated_at") VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11) RETURNING "id"  [["content", ""], ["created_at", Mon, 04 May 2015 09:37:40 UTC +00:00], ["highlight", ""], ["locale", "es"], ["meta_description", nil], ["meta_keywords", nil], ["name", ""], ["post_id", 286], ["slug", ""], ["title", nil], ["updated_at", Mon, 04 May 2015 09:37:40 UTC +00:00]]


 [select ... ]
  Rendered widgets/_list.html.erb (33.9ms)
  Rendered widgets/create.js.coffee (1475.1ms)
Completed 200 OK in 1540ms (Views: 1356.5ms | ActiveRecord: 164.5ms)


Started POST "/backend/posts" for ***.***.***.*** at 2015-05-04 11:37:42 +0200
Processing by PostsController#create as JS
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"********=", "post"=>{"widget_id"=>"8", "homepage"=>"1", "active"=>"1", "translations_attributes"=>{"1"=>{"id"=>"", "locale"=>"ca", "name"=>"prueba", "highlight"=>"<p>hola<br></p>", "content"=>"<p>hola<br></p>"}, "2"=>{"id"=>"", "locale"=>"es", "name"=>"", "highlight"=>"", "content"=>""}}}, "commit"=>"Ejecutar"}
  [1m[36mCustomer Load (0.9ms)[0m  [1mSELECT "customers".* FROM "customers" WHERE "customers"."id" = $1 LIMIT 1[0m  [["id", 3]]
  [1m[35mLang Load (0.7ms)[0m  SELECT "langs".* FROM "langs" WHERE "langs"."customer_id" = 3 AND "langs"."default" = 't' LIMIT 1
  [1m[36m (0.4ms)[0m  [1mBEGIN[0m
  [1m[35mSQL (0.9ms)[0m  INSERT INTO "posts" ("active", "created_at", "header", "homepage", "updated_at", "widget_id") VALUES ($1, $2, $3, $4, $5, $6) RETURNING "id"  [["active", true], ["created_at", Mon, 04 May 2015 09:37:42 UTC +00:00], ["header", nil], ["homepage", true], ["updated_at", Mon, 04 May 2015 09:37:42 UTC +00:00], ["widget_id", 8]]
  [1m[36mSQL (1.0ms)[0m  [1mINSERT INTO "post_translations" ("content", "created_at", "highlight", "locale", "meta_description", "meta_keywords", "name", "post_id", "slug", "title", "updated_at") VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11) RETURNING "id"[0m  [["content", "<p>hola<br></p>"], ["created_at", Mon, 04 May 2015 09:37:42 UTC +00:00], ["highlight", "<p>hola<br></p>"], ["locale", "ca"], ["meta_description", nil], ["meta_keywords", nil], ["name", "prueba"], ["post_id", 287], ["slug", "prueba"], ["title", nil], ["updated_at", Mon, 04 May 2015 09:37:42 UTC +00:00]]
  [1m[35mSQL (0.8ms)[0m  INSERT INTO "post_translations" ("content", "created_at", "highlight", "locale", "meta_description", "meta_keywords", "name", "post_id", "slug", "title", "updated_at") VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11) RETURNING "id"  [["content", ""], ["created_at", Mon, 04 May 2015 09:37:42 UTC +00:00], ["highlight", ""], ["locale", "es"], ["meta_description", nil], ["meta_keywords", nil], ["name", ""], ["post_id", 287], ["slug", ""], ["title", nil], ["updated_at", Mon, 04 May 2015 09:37:42 UTC +00:00]]
  [1m[36mPost::Translation Load (0.9ms)[0m  [1mSELECT "post_translations".* FROM "post_translations" WHERE "post_translations"."id" = $1 LIMIT 1[0m  [["id", 564]]
  [1m[35mPost::Translation Load (0.7ms)[0m  SELECT "post_translations".* FROM "post_translations" WHERE "post_translations"."id" = $1 LIMIT 1  [["id", 565]]
  [1m[36m (19.8ms)[0m  [1mCOMMIT[0m

插入执行的时间相差 2 秒。

谁能帮帮我?

谢谢!

【问题讨论】:

  • 你的应用日志是什么?是否有两个帖子请求发送到服务器?
  • 是的,两个帖子。问题编辑。
  • 仔细检查并确保您的模型上没有任何 before_createafter_create 触发器,这些触发器被编码为保存新对象。有一次我看到有人使用回调来创建触发回调的对象,从而复制了记录(同时有一些......奇怪的递归问题)

标签: ruby-on-rails ruby postgresql-9.1


【解决方案1】:

查看您的表单:

<dt><%= l.label :name %></dt>
<dd><%= l.text_field :name %></dd>
<dt><%= l.label :highlight %></dt>

不应该是:

<%= l.submit "#{smart_label(request.fullpath)}", :class => 'btn green' %>

为您提交? 但是&lt;% end %&gt; 建议您以某种方式嵌套您的表单,因此请随时发布您的完整表单。

正如 Marek 所提到的,为您的 create 操作提供一些日志会很有帮助。


您的创建操作所需的响应是什么?如果您想调用 views/posts/create.js 尝试将其添加到您的 create 操作中:

def create
  post = Post.new(params[:post])
  respond_to do |format|
    if post.save
      format.js { layout: false }
      flash[:notice] = "El post se ha creado correctamente."
      @page = post.widget.page
    end
 end
 @object = post
 render 'widgets/create'
end

【讨论】:

    【解决方案2】:

    首先谢谢。

    在我在 firebug 中看到有 2 个 post 调用之后,我看到了 create 执行了 2 次的日志。

    查看应用程序中有 2 个 jquery 的萤火虫,因此创建执行了两次。

    当我删除 application.js 中的 jquery 时,错误已得到纠正。

    谢谢大家!

    【讨论】:

      猜你喜欢
      • 2014-09-23
      • 2014-02-02
      • 1970-01-01
      • 2021-05-05
      • 2023-03-10
      • 2014-07-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多