[英]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.