简体   繁体   English

使用AJAX渲染的部分内容无法正常工作

[英]Partial rendered with AJAX not working correctly

I render this partial with AJAX: 我用AJAX渲染了这个部分:

<% if current_user.profiles_shown_video.where("video_id = ?", params[:id]).count == 0 %>
  <p class="none"> None </p>
<% else %>
  <ul class="shown_users_list">
    <% current_user.profiles_shown_video.where("video_id = ?", params[:id]).each do |p| %>  
      <li><%= link_to image_tag(p.photo.url(:thumbnail)), profile_path(p), :class => "feed_image"%> 
          <%= link_to "#{p.user.name}", profile_path(p), :class => "normal squeeze" %> 
          <%= link_to image_tag("/images/facebox/closelabel.gif"), showable_video_path(ShowableVideo.find_by_user_id_and_profile_id_and_video_id(current_user, p, @video)), :method => :delete, :remote => true, :class => "showable_video_delete" %></li>
    <% end %>
  </ul> 
<% end %>

For some reason, the first if statement is always being evaluated to true, even if it shouldn't be. 由于某些原因,第一个if语句始终被评估为true,即使不应该如此。 In other words, <p class="none"> None </p> is always being displayed. 换句话说,始终显示<p class="none"> None </p> If I refresh the page, it gets corrected. 如果刷新页面,它将得到纠正。 Why is the if statement of this partial not being evaluated correctly when rendered with AJAX? 为什么使用AJAX渲染时,此部分的if语句无法正确评估? How can I fix this? 我怎样才能解决这个问题?

Here's the destroy action: 这是销毁动作:

def destroy
  @showable_video = current_user.showable_videos.find(params[:id])
  @showable_video.destroy
  respond_to do |format|
     format.html {redirect_to :back}
     format.js
   end
end

This is in destroy.js.erb. 这是在destroy.js.erb中。 It renders a partial containing the first block of code above: 它呈现了包含上面的第一段代码的部分代码:

$("#shown_users_div").html('<%= escape_javascript(render("showable_videos/shown_users")) %>')

ActiveRecord caches associated objects. ActiveRecord缓存关联的对象。 After running @showable_video.destroy , current_user (itself probably being cached) will still have @showable_video in memory, so the collection will be non-empty. 运行@showable_video.destroycurrent_user (可能正在缓存)仍将在内存中保留@showable_video ,因此该集合将为非空。 After creating, updating, or deleting the database record, you must run: 创建,更新或删除数据库记录后,必须运行:

current_user.reload

so that it reloads associated objects from the database. 以便它从数据库中重新加载关联的对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM