【问题标题】:Rails 4 ajax refreshes partial only after submitting form twiceRails 4 ajax仅在提交表单两次后刷新部分
【发布时间】:2014-01-22 11:47:02
【问题描述】:

我是rails 的新手,并且一直在开发一个类似 Twitter 的简单网站。

在我的推文页面中,当新推文表单(使用 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

我的 _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 %>");

我的终端登录创建一条推文“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)

【问题讨论】:

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


    【解决方案1】:

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

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

    将分页过程移动到单独的私有方法可能会更干净

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-05-08
      • 1970-01-01
      • 2016-04-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多