[英]Ajax Unobtrusive not working when posting from rendered partial view
[英]Partial rendered with AJAX not working correctly
我用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 %>
由于某些原因,第一个if语句始终被评估为true,即使不应该如此。 换句话说,始终显示<p class="none"> None </p>
。 如果刷新页面,它将得到纠正。 为什么使用AJAX渲染时,此部分的if语句无法正确评估? 我怎样才能解决这个问题?
这是销毁动作:
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
这是在destroy.js.erb中。 它呈现了包含上面的第一段代码的部分代码:
$("#shown_users_div").html('<%= escape_javascript(render("showable_videos/shown_users")) %>')
ActiveRecord缓存关联的对象。 运行@showable_video.destroy
, current_user
(可能正在缓存)仍将在内存中保留@showable_video
,因此该集合将为非空。 创建,更新或删除数据库记录后,必须运行:
current_user.reload
以便它从数据库中重新加载关联的对象。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.