关于帖子的AJAX Feed更新

时间:2012-02-07 09:28:04

标签: ruby-on-rails

我完成了Hartl的Rails教程书,现在我正在尝试学习更多内容。我试图在提交后功能上添加Feed更新。这种方法运作良好,只有一个例外。出于某种原因,每次单击“提交”按钮时,都会向数据库添加两个条目(重复项)。基本上,如果我查看终端中的服务器对话框,我会看到其中的两个:

Started POST "/microposts" for 127.0.0.1 at 2012-02-07 22:17:41 +1300
Creating scope :from_users_followed_by. Overwriting existing method Micropost.from_users_followed_by.
  Processing by MicropostsController#create as JS
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"p2pZx4VtIOSnywyVrXnk0vOpI5WdGVeuhLTfEaXmT6o=", "micropost"=>{"content"=>"This is a test post!"}, "commit"=>"Submit"}
  User Load (0.4ms)  SELECT "users".* FROM "users" WHERE "users"."id" = 19 LIMIT 1
   (0.1ms)  BEGIN
  SQL (0.4ms)  INSERT INTO "microposts" ("content", "created_at", "updated_at", "user_id") VALUES ($1, $2, $3, $4) RETURNING "id"  [["content", "This is a test post!"], ["created_at", Tue, 07 Feb 2012 09:17:42 UTC +00:00], ["updated_at", Tue, 07 Feb 2012 09:17:42 UTC +00:00], ["user_id", 19]]
   (13.6ms)  COMMIT
   (0.5ms)  SELECT COUNT(*) FROM "microposts" WHERE (user_id IN (SELECT followed_id FROM relationships
 WHERE follower_id = 19) OR user_id = 19)
  Micropost Load (0.5ms)  SELECT "microposts".* FROM "microposts" WHERE (user_id IN (SELECT followed_id FROM relationships
 WHERE follower_id = 19) OR user_id = 19) ORDER BY microposts.created_at DESC LIMIT 5 OFFSET 0
  CACHE (0.0ms)  SELECT COUNT(*) FROM "microposts" WHERE (user_id IN (SELECT followed_id FROM relationships
 WHERE follower_id = 19) OR user_id = 19)
  User Load (0.2ms)  SELECT "users".* FROM "users" WHERE "users"."id" = 19 LIMIT 1
  CACHE (0.0ms)  SELECT "users".* FROM "users" WHERE "users"."id" = 19 LIMIT 1
  CACHE (0.0ms)  SELECT "users".* FROM "users" WHERE "users"."id" = 19 LIMIT 1
  CACHE (0.0ms)  SELECT "users".* FROM "users" WHERE "users"."id" = 19 LIMIT 1
  CACHE (0.0ms)  SELECT "users".* FROM "users" WHERE "users"."id" = 19 LIMIT 1
Rendered shared/_feed_item.html.erb (11.7ms)
Rendered shared/_feed.html.erb (20.4ms)
   (0.3ms)  SELECT COUNT(*) FROM "microposts" WHERE "microposts"."user_id" = 19
Rendered microposts/create.js.erb (23.3ms)
Completed 200 OK in 103ms (Views: 26.9ms | ActiveRecord: 23.4ms)

这是在我的微博控制器中:

  def create
    @micropost  = current_user.microposts.build(params[:micropost])
    if 
      @micropost.save!
      respond_to do |format|
        format.html { redirect_to root_path }
        format.js
      end
    end
  end

这是我的create.js.erb

$(".ajaxreloadposts").html("<%= escape_javascript(render('shared/feed')) %>")

这是来自我的home.html.erb

<% if signed_in? %>
  <table class="front" summary="For signed-in users">
    <tr>
      <td class="main">
        <h1 class="micropost">Create Post:</h1>
        <%= render 'shared/micropost_form' %>
        <span class="ajaxreloadposts">
        <%= render 'shared/feed' %>
        </span>
      </td>
      <td class="sidebar round">
        <%= render 'shared/user_info' %>
        <%= render 'shared/stats' %>
      </td>
    </tr>
  </table>
<% else %>

最后,这是我的_feed部分

<% @feed_items = current_user.feed.paginate(:page => params[:page], :per_page => 5) %>
<% unless @feed_items.empty? %>
  <table class="microposts" summary="User microposts">
    <%= render :partial => 'shared/feed_item', :collection => @feed_items %>
  </table>
  <div class="digg_pagination">
  <%= will_paginate @feed_items %>
  </div>
<% end %>

有趣的是,分页按钮也会在AJAX更新时重新加载错误..并采用microposts?=的形式而不是所需的格式page?=,但这完全是另一回事。

1 个答案:

答案 0 :(得分:0)

好像你的ajax请求被触发了两次。检查您的应用程序js并确保js仅包含一次。而不是要求树,明确地要求j。