我想更新我的「點子」網頁喜歡的號碼,當有人用ajax綁定Ajax請求LINK_TO
的按鈕顯示爲理念#如下顯示視圖(路徑當前擊中像按鈕定向到喜歡#創建沒有Ajax)
- unless signed_in? and current_user.likes? @idea
= link_to "Like this idea!", idea_likes_path(@idea), :class => "btn btn-large btn-block btn-success", method: :post, id: 'like_button'
- else
= link_to "Unlike this idea", idea_like_path(@idea, current_user.idea_like(@idea)), :class => "btn btn-large btn-block btn-inverse", method: :delete
我的application.js具有如下:
jQuery.fn.ajaxFormSubmit = function() {
this.submit(function() {
$.post(this.action, $(this).serialize(), null, "script");
return false;
})
return this;
};
$(document).ready(function() {
$("#like_button").ajaxFormSubmit();
})
的LikesController#創建方法:
def create
like = Like.new
@idea.likes << like
current_user.likes << like
respond_to do |format|
format.html { redirect_to @idea }
format.js
end
end
終於create.js.erb:
$("#like_count").html("<%= pluralize(@ideas.likes.count, 'Like') %>");
與上面的代碼,該按鈕依然刷新整個頁面。我如何讓AJAX工作?
當我添加遠程:true我得到一個ActionView :: MissingTemplate錯誤 – delisdeli
沒關係!我的視圖文件夾是喜歡而不是喜歡:( – delisdeli