简体   繁体   English

充当不更新Rails的Voable Ajax投票大小

[英]Acts As Votable Ajax Vote Size Not Updating Rails

I am using the acts as votable gem on my rails 4 app. 我在Rails 4应用程序中将其作为可食用的宝石。

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. 我终于使用ajax使其工作了,因此当用户投票时页面不会刷新,但是,直到刷新页面后,vote.size才会更新。

Here is my controller action: 这是我的控制器动作:

  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

and view: 并查看:

<%= 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>

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". 到目前为止,当用户投票时,它完全摆脱了“有用的”按钮并增加了投票的大小,并显示html“感谢您投票”。

I am having trouble figuring out how to keep the button and simply update the upvote.size to the correct number. 我在弄清楚如何保留按钮并将简单地将upvote.size更新为正确的数字时遇到了麻烦。 I have tried assigning a class to the upvote size and then using something like this: $('.item-class').html(data.count) but no luck. 我尝试将一个类分配给upvote大小,然后使用类似这样的东西: $('.item-class').html(data.count)但没有运气。

Any recommendations? 有什么建议吗? Thank you! 谢谢!

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

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

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) 替换按钮的原因是要替换“ this”的html(定义为用.vote类标记的整个部分)

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. 如果您只想替换.vote部分中的item-class(即保持按钮原样),则需要a)添加带有“ item-class”类的东西,b)减少要替换的东西。 eg: 例如:

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

(note I've not bug-tested this - you may need to google javascript first to make sure this is the correct usage). (请注意,我尚未对此进行错误测试-您可能需要先使用Google javascript来确保这是正确的用法)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM