所以我遇到的问题是让我喜欢/不象按钮与阿贾克斯在我的Ruby on Rails应用程序刷新,这里是我的代码:Ruby on Rails的,如何在像使用Ajax更新DIV /不像
应用/视图/ _comment.html.haml
- likes = comment.likes
%div.comment{id: "comment-#{comment.id}"}
.comment-avatar
.medium-user-avatar.avatar-canvas
- if comment.user.avatar_url
= image_tag comment.user.avatar_url(:medium)
- else
%span.medium-user-initials.initials-decoration
= comment.user.avatar_initials
%span.comment-username= link_to(comment.user_name, "#")
%span.comment-body~ markdown(comment.body)
.comment-time
= time_ago_in_words(comment.created_at) + " ago"
- if can? :like, comment
= " · "
- if likes.find_by_user_id(current_user.id).nil?
= link_to "Like", like_comment_path(comment), method: :post, remote: true
- else
= link_to "Unlike", unlike_comment_path(comment), method: :post, remote: true
- if comment.user == current_user
= " · "
= link_to "Delete", comment_path(comment), method: :delete, remote: true,
: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
应用程序/控制器/ comments_controller.rb
class CommentsController < BaseController
load_and_authorize_resource
def destroy
destroy!{ discussion_url(resource.discussion) }
end
def like
comment_vote = resource.like current_user
Event.comment_liked!(comment_vote)
#redirect_to discussion_url(resource.discussion)
render :partial => "like"
comment_likes
end
def unlike
resource.unlike current_user
#redirect_to discussion_url(resource.discussion)
render :partial => "unlike"
comment_likes
end
def comment_likes
render :partial => "comment_likes"
end
end
,然后像.js.erb文件,不像和comment_likes:
应用程序/视图/ _like.js.erb
$(".comment-time a#like").html("<%= escape_javascript(render('.comment-time a#like'")
app/vi EWS/_unlike.js.erb
$(".comment-time a#unlike").html("<%= escape_javascript(render('.comment-time a#unlike'")
应用程序/视图/ _comment_likes.js.erb
$(".comment-likes a##").html("<%= escape_javascript(render('.comment-likes a##'")
目前点击喜欢将更新数据库,但在页面刷新之前不会显示更改,我只想用ajax刷新个别div。有关div的更多信息可能会有所帮助,所以ruby创建html并包含在该示例中,或者当已经喜欢时,我只需刷新这些div以显示来自数据库的最新内容以及包含http:// localhost :3000 /评论/ 7 /不像500(内部服务器错误)“
脚本的其余部分已完成coffeescript如果重要?我读的控制器功能应该使用.js.erb所以希望这不是' (我确定我的js.erb是错误的)
感谢您会喜欢一些更好的导游在各处搜索网页! –