充当不更新Rails的Voable Ajax投票大小 [英] Acts As Votable Ajax Vote Size Not Updating Rails

查看:123
本文介绍了充当不更新Rails的Voable Ajax投票大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在Rails 4应用程序中将其作为可食用的宝石.

I am using the acts as votable gem on my rails 4 app.

我终于使用ajax使其工作了,所以当用户投票时页面不会刷新,但是,直到刷新页面后,vote.size才会更新.

I have finally gotten it to work using ajax, so the page does not refresh when a user votes, however, the vote.size doesn't update until the page is refreshed.

这是我的控制器动作:

  def upvote 
    @article = Article.find(params[:article_id])
    @subarticle = @article.subarticles.find(params[:id])
    session[:voting_id] = request.remote_ip
    voter = Session.find_or_create_by(ip: session[:voting_id])
    voter.likes @subarticle
    respond_to do |format|
      format.html {redirect_to :back }
      format.json { render json: { count: @subarticle.get_upvotes.size } }
    end
  end

并查看:

<%= link_to like_article_subarticle_path(@article, subarticle), class: "vote", method: :put, remote: true, data: { type: :json } do %>
  <button type="button" class="btn btn-success btn-lg" aria-label="Left Align" style="margin-right:5px">
    <span class="glyphicon glyphicon-thumbs-up" aria-hidden="true"></span> Helpful
  </button><span class="badge" style="margin-right:10px"><%= subarticle.get_upvotes.size %></span>
<% end %>

<script>
    $('.vote')
  .on('ajax:send', function () { $(this).addClass('loading'); })
  .on('ajax:complete', function () { $(this).removeClass('loading'); })
  .on('ajax:error', function () { $(this).after('<div class="error">There was an issue.</div>'); })
  .on('ajax:success', function(e, data, status, xhr) { $(this).html("Thank you for voting!"); });
</script>

到目前为止,当用户投票时,它完全摆脱了有用"按钮和投票大小,并显示html感谢您投票".

As of now, when a user votes, it completely get's rid of the "helpful" button and upvote size, and displays the html"Thank you for voting".

我在弄清楚如何保留按钮并将简单地将upvote.size更新为正确的数字时遇到了麻烦.我尝试将一个类分配给upvote大小,然后使用类似以下的内容:$('.item-class').html(data.count)但没有运气.

I am having trouble figuring out how to keep the button and simply update the upvote.size to the correct number. I have tried assigning a class to the upvote size and then using something like this: $('.item-class').html(data.count) but no luck.

有什么建议吗?谢谢!

推荐答案

$('.item-class').html(data.count)

item-class似乎没有在模板中的任何地方声明……但是即使这样,如果您这样调用它,它也可能会与页面上的多个内容匹配,因此这种替换不会成功.工作.

item-class doesn't seem to be declared in your template anywhere... but even if it did, if you call it like this, it will likely match more than one thing on the page, so this replace won't work.

替换按钮的原因是要替换"this"(定义为用.vote类标记的整个部分)的html

The reason why the button is being replaced is you are replacing the html of "this" (which is defined as the whole section marked with the class .vote)

如果您只想替换.vote部分中的item-class(即保持按钮原样),则需要a)添加带有'item-class'类的东西,b)减少要替换的东西对此.例如:

If you want to replace just the item-class within the .vote section (ie leave the button intact) then you need to a) add something with the class of 'item-class' and b) reduce what you are replacing to that. eg:

$(this).first('.item-class').html("Thank you for voting!");

(请注意,我尚未对此进行错误测试-您可能需要先使用Google javascript来确保这是正确的用法).

(note I've not bug-tested this - you may need to google javascript first to make sure this is the correct usage).

这篇关于充当不更新Rails的Voable Ajax投票大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆