Partial rendered with AJAX not working correctly
I render this partial with 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_u开发者_如何转开发ser.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. In other words, <p class="none"> None </p>
is always being displayed. If I refresh the page, it gets corrected. Why is the if statement of this partial not being evaluated correctly when rendered with AJAX? 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. 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. 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. After creating, updating, or deleting the database record, you must run:
current_user.reload
so that it reloads associated objects from the database.
精彩评论