只有在提交表单两次后,Rails 4 ajax才会刷新部分内容

时间:2014-01-22 11:47:02

标签: jquery ruby-on-rails ajax render kaminari

我是rails的新手,并且一直致力于像网站这样简单的推特。

在我的推文页面中,当新的推文表单(带有ajax)被填充并提交后,部分(显示推文)没有改变,再次单击提交时,部分刷新并显示一条额外的推文,即使有两条新推文已经创建(通过提交两次)。

如果在首次提交后,我在浏览器中刷新页面,我可以在列表中看到新创建的推文。

终端日志显示两条推文,其内容相同。

我对jquery-rails使用javascript

我使用Kaminari作为我的分页和评论它解决问题,就像删除AJAX一样。

这是我的代码

我的推文控制器创建操作

 def create

     @tweet = Tweet.new(tweet_params)

     @tweetse=Tweet.all.order('created_at DESC')



 @tweets=Kaminari.paginate_array(@tweetse).page(params[:page]).per(8)
   #for my search box
    @tw = Tweet.order('created_at DESC').search(params[:search]) 
    if @tw== nil
      @tweets = Tweet.all.page(params[:page]).per(8)
    else
   @tweets=Kaminari.paginate_array(@tw).page(params[:page]).per(8)

  end


    @tweet.user=current_user

    respond_to do |format|
      if @tweet.save
        format.html { redirect_to tweets_url, notice: 'Tweet was successfully created.' }
        format.json { render action: 'show', status: :created, location: @tweet }
        format.js 

      else
        format.html { render action: 'new' }
        format.json { render json: @tweet.errors, status: :unprocessable_entity }
      end
    end

my _form偏新推文

<div id ="forms">
<%= simple_form_for(@tweet,:remote=> true) do |f| %>
  <%= f.error_notification %>

  <div class="form-input">
    <%= f.input :content %>
    <% #@tweet.user =current_user%>
  </div>

  <div class="form-actions">
  <br>

   <button> <%= f.button :submit, :class =>"tweetbutton" %></button>
  </div>

<br>
<% end %>
</div>

我的推文索引页

<h1>Listing all tweets</h1>


<div id="forms">

<%=render 'form'%>
</div>
<div>
<%= form_tag tweets_url, :method => 'get',:remote=>true do %>
  <p>
    <%= text_field_tag :search, params[:search] %>
   <button> <%= submit_tag "Search",:remote=>true, :content => nil, :class =>"tweetbutton" %></button>
  </p>
<% end %>


</div>
<h3><%= paginate @tweets %></h3>

<div id="posts">

<%= render 'tweets' %>

</div>

<h3><%= paginate @tweets %></h3>

我的推文部分(_tweets)

<div id="post">
<table>
  <thead>
  <td></td>
    <tr>
      <th>Content</th>
      <th>Author</th>
      <th></th>
      <th></th>
      <th></th>
    </tr>

  </thead>
  <tbody>

    <% @tweets.each do |tweet| %>

      <tr>
        <td><br><br><h3><%= tweet.content %></h3></td>
        <td><br><br><h6><%=link_to tweet.user.name.titleize ,tweet.user %></h6></td>
        <td><br><br><%= link_to 'Show', tweet, :class =>"button2" %></td>
        <% unless current_user!=tweet.user%>


        <td><br><br><%= link_to 'Edit', edit_tweet_path(tweet), :class =>"button1" %></td>
        <td><br><br><%= button_to 'Delete', tweet, :remote => true, method: :delete, data: { confirm: 'Are you sure?' }, :class =>"button3" %></td>
        <% end %>
       </tr>
        <p>                       </p>
        <p></p>
    <% end %>

  </tbody>

</table>

<br>
</div>

我的create.js在创建操作后呈现

 $('#posts').html("<%= escape_javascript(render(:partial => 'tweets')).html_safe %>");

我的终端登录创建Tweet“HELLO WORLD”并按两次“提交”按钮。 已经创建了两条推文(通过提交两次),但只有一条推文显示在部分

Started POST "/tweets" for 127.0.0.1 at 2014-01-22 17:11:24 +0530
Processing by TweetsController#create as JS
  Parameters: {"utf8"=>"√", "tweet"=>{"content"=>"HELLO WORLD"}}
  User Load (1.0ms)  SELECT "users".* FROM "users" WHERE "users"."id" = 5 ORDER
BY "users"."id" ASC LIMIT 1
  Tweet Load (5.0ms)  SELECT "tweets".* FROM "tweets" ORDER BY created_at DESC
DEPRECATION WARNING: Calling #find(:all) is deprecated. Please call #all directl
y instead. (called from D:in `find':)
DEPRECATION WARNING: Relation#all is deprecated. If you want to eager-load a rel
ation, you can call #load (e.g. `Post.where(published: true).load`). If you want
 to get an array of records from a relation, you can call #to_a (e.g. `Post.wher
e(published: true).to_a`). (called from D:in `find':)
  CACHE (0.0ms)  SELECT "tweets".* FROM "tweets" ORDER BY created_at DESC
   (0.0ms)  begin transaction
  SQL (3.0ms)  INSERT INTO "tweets" ("content", "created_at", "updated_at", "use
r_id") VALUES (?, ?, ?, ?)  [["content", "HELLO WORLD"], ["created_at", Wed, 22
Jan 2014 11:41:24 UTC +00:00], ["updated_at", Wed, 22 Jan 2014 11:41:24 UTC +00:
00], ["user_id", 5]]
  SQL (1.0ms)  INSERT INTO "activities" ("created_at", "key", "owner_id", "owner
_type", "parameters", "trackable_id", "trackable_type", "updated_at") VALUES (?,
 ?, ?, ?, ?, ?, ?, ?)  [["created_at", Wed, 22 Jan 2014 11:41:24 UTC +00:00], ["
key", "tweet.create"], ["owner_id", 5], ["owner_type", "User"], ["parameters", "
--- {}\n"], ["trackable_id", 440], ["trackable_type", "Tweet"], ["updated_at", W
ed, 22 Jan 2014 11:41:24 UTC +00:00]]
   (9.0ms)  commit transaction
DEPRECATION WARNING: The file C:/Sites/2014/Twitter/app/views/tweets/_tweets did
 not specify a template handler. The default is currently ERB, but will change t
o RAW in the future. (called from _app_views_tweets_create_js___531005842_370065
48 at C:/Sites/2014/Twitter/app/views/tweets/create.js:5)
  User Load (0.0ms)  SELECT "users".* FROM "users" WHERE "users"."id" = ? ORDER
BY "users"."id" ASC LIMIT 1  [["id", 5]]
  CACHE (0.0ms)  SELECT "users".* FROM "users" WHERE "users"."id" = ? ORDER BY "
users"."id" ASC LIMIT 1  [["id", 5]]
  CACHE (0.0ms)  SELECT "users".* FROM "users" WHERE "users"."id" = ? ORDER BY "
users"."id" ASC LIMIT 1  [["id", 5]]
  CACHE (0.0ms)  SELECT "users".* FROM "users" WHERE "users"."id" = ? ORDER BY "
users"."id" ASC LIMIT 1  [["id", 5]]
  CACHE (0.0ms)  SELECT "users".* FROM "users" WHERE "users"."id" = ? ORDER BY "
users"."id" ASC LIMIT 1  [["id", 5]]
  CACHE (0.0ms)  SELECT "users".* FROM "users" WHERE "users"."id" = ? ORDER BY "
users"."id" ASC LIMIT 1  [["id", 5]]
  CACHE (0.0ms)  SELECT "users".* FROM "users" WHERE "users"."id" = ? ORDER BY "
users"."id" ASC LIMIT 1  [["id", 5]]
  CACHE (0.0ms)  SELECT "users".* FROM "users" WHERE "users"."id" = ? ORDER BY "
users"."id" ASC LIMIT 1  [["id", 5]]
  Rendered tweets/_tweets (47.0ms)
DEPRECATION WARNING: The file C:/Sites/2014/Twitter/app/views/tweets/_tweets did
 not specify a template handler. The default is currently ERB, but will change t
o RAW in the future. (called from _app_views_tweets_create_js___531005842_370065
48 at C:/Sites/2014/Twitter/app/views/tweets/create.js:10)
  Rendered tweets/_tweets (13.0ms)
  Rendered tweets/create.js (76.0ms)
Completed 200 OK in 202ms (Views: 109.0ms | ActiveRecord: 19.0ms | Solr: 0.0ms)


Started POST "/tweets" for 127.0.0.1 at 2014-01-22 17:11:31 +0530
Processing by TweetsController#create as JS
  Parameters: {"utf8"=>"√", "tweet"=>{"content"=>"HELLO WORLD"}}
  User Load (1.0ms)  SELECT "users".* FROM "users" WHERE "users"."id" = 5 ORDER
BY "users"."id" ASC LIMIT 1
  Tweet Load (7.0ms)  SELECT "tweets".* FROM "tweets" ORDER BY created_at DESC
DEPRECATION WARNING: Calling #find(:all) is deprecated. Please call #all directl
y instead. (called from D:in `find':)
DEPRECATION WARNING: Relation#all is deprecated. If you want to eager-load a rel
ation, you can call #load (e.g. `Post.where(published: true).load`). If you want
 to get an array of records from a relation, you can call #to_a (e.g. `Post.wher
e(published: true).to_a`). (called from D:in `find':)
  CACHE (0.0ms)  SELECT "tweets".* FROM "tweets" ORDER BY created_at DESC
   (0.0ms)  begin transaction
  SQL (4.0ms)  INSERT INTO "tweets" ("content", "created_at", "updated_at", "use
r_id") VALUES (?, ?, ?, ?)  [["content", "HELLO WORLD"], ["created_at", Wed, 22
Jan 2014 11:41:31 UTC +00:00], ["updated_at", Wed, 22 Jan 2014 11:41:31 UTC +00:
00], ["user_id", 5]]
  SQL (1.0ms)  INSERT INTO "activities" ("created_at", "key", "owner_id", "owner
_type", "parameters", "trackable_id", "trackable_type", "updated_at") VALUES (?,
 ?, ?, ?, ?, ?, ?, ?)  [["created_at", Wed, 22 Jan 2014 11:41:31 UTC +00:00], ["
key", "tweet.create"], ["owner_id", 5], ["owner_type", "User"], ["parameters", "
--- {}\n"], ["trackable_id", 441], ["trackable_type", "Tweet"], ["updated_at", W
ed, 22 Jan 2014 11:41:31 UTC +00:00]]
   (13.0ms)  commit transaction
DEPRECATION WARNING: The file C:/Sites/2014/Twitter/app/views/tweets/_tweets did
 not specify a template handler. The default is currently ERB, but will change t
o RAW in the future. (called from _app_views_tweets_create_js___531005842_370065
48 at C:/Sites/2014/Twitter/app/views/tweets/create.js:5)
  User Load (0.0ms)  SELECT "users".* FROM "users" WHERE "users"."id" = ? ORDER
BY "users"."id" ASC LIMIT 1  [["id", 5]]
  CACHE (0.0ms)  SELECT "users".* FROM "users" WHERE "users"."id" = ? ORDER BY "
users"."id" ASC LIMIT 1  [["id", 5]]
  CACHE (0.0ms)  SELECT "users".* FROM "users" WHERE "users"."id" = ? ORDER BY "
users"."id" ASC LIMIT 1  [["id", 5]]
  CACHE (1.0ms)  SELECT "users".* FROM "users" WHERE "users"."id" = ? ORDER BY "
users"."id" ASC LIMIT 1  [["id", 5]]
  CACHE (0.0ms)  SELECT "users".* FROM "users" WHERE "users"."id" = ? ORDER BY "
users"."id" ASC LIMIT 1  [["id", 5]]
  CACHE (0.0ms)  SELECT "users".* FROM "users" WHERE "users"."id" = ? ORDER BY "
users"."id" ASC LIMIT 1  [["id", 5]]
  CACHE (0.0ms)  SELECT "users".* FROM "users" WHERE "users"."id" = ? ORDER BY "
users"."id" ASC LIMIT 1  [["id", 5]]
  CACHE (0.0ms)  SELECT "users".* FROM "users" WHERE "users"."id" = ? ORDER BY "
users"."id" ASC LIMIT 1  [["id", 5]]
  Rendered tweets/_tweets (62.0ms)
DEPRECATION WARNING: The file C:/Sites/2014/Twitter/app/views/tweets/_tweets did
 not specify a template handler. The default is currently ERB, but will change t
o RAW in the future. (called from _app_views_tweets_create_js___531005842_370065
48 at C:/Sites/2014/Twitter/app/views/tweets/create.js:10)
  Rendered tweets/_tweets (18.0ms)
  Rendered tweets/create.js (104.0ms)
Completed 200 OK in 279ms (Views: 151.0ms | ActiveRecord: 27.0ms | Solr: 0.0ms)

1 个答案:

答案 0 :(得分:0)

这是因为@tweetse查询在@tweet对象保存之前执行了。

@tweetse查询在Kaminari.paginate_array使用时执行,所以将分页移到@ tweet.save之后应该修复它:)

将分页过程移动到分离的私有方法

也可能更清晰