我有一个使用Rails 5的应用程序,我有一个文章资源,我正在实施"喜欢/不像"功能。我已经有了这个功能,但我在更新视图上的@artlike
变量时遇到了麻烦。这些是我的文件:
show.html.erb:
<div class="pull-right">
<i class="fa fa-diamond"></i> <span id="likes"><%= render partial: "likes", locals: {artlike: @artlike} %></span>
</div>
控制器:
respond_to :js
def like
puts "LikesController:like"
@usuario = current_usuario
@articulo = Articulo.find(params[:id])
@usuario.like!(@articulo)
@artlike = Like.where(articulo_id: @articulo.id).count
end
def unlike
puts "LikesController:unlike"
@usuario = current_usuario
@like = @usuario.likes.find_by_articulo_id(params[:id])
@articulo = Articulo.find(params[:id])
@like.destroy!
@artlike = Like.where(articulo_id: @articulo.id).count
end
_likes.html.erb:
<span id="likes"><%= @artlike %></span>
like.js.erb文件
$('span#likes').html('<%= j render partial: "likes", locals: {artlike: @artlike} %>');
如何在视图中执行@artlike
值?
这是控制台日志中的输出: