所以我在我的评论下有一个带有类似按钮的页面,并且我试图找出如何在不刷新整个页面的情况下执行此操作,目前代码看起来像这样:
- if can? :like, comment
= " · "
- if likes.find_by_user_id(current_user.id).nil?
= link_to "Like", like_comment_path(comment), method: :post
- else
= link_to "Unlike", unlike_comment_path(comment), method: :post
- if comment.user == current_user
= " · "
= link_to "Delete", comment_path(comment), method: :delete,
:data => { :confirm => "Are you sure you want to delete this comment?" }
- if likes.count > 0
.comment-likes
- likers = likes.map { |like| link_to(like.user_name, "#") }
- if likers.length > 1
- likers = likers.slice(0, likers.length - 1).join(", ").concat(" and " + likers.slice(-1))
- else
- likers = likers[0]
= "Liked by #{likers}".html_safe
更改类似和不同的方法:post to:update似乎有一个很大的不同是这个函数使用ajax和:post not?如果没有,那么我将如何将其变成ajax函数。
继承我的comments_controller.rb东西
def like
comment_vote = resource.like current_user
Event.comment_liked!(comment_vote)
redirect_to discussion_url(resource.discussion)
end
def unlike
resource.unlike current_user
redirect_to discussion_url(resource.discussion)
end
答案 0 :(得分:3)
= link_to "Like", like_comment_path(comment), method: :post, remote: true
然后在你的likes_controller创建动作,respond_to format.js
def create
respond_to do |format|
format.html { .. your liking code .. }
format.js {.. your liking code .. }
end
end
然后,这将在您的视图文件夹中调用create.js.erb,您可以从那里动态更新您的html