本文介绍了成功的 Ajax 请求以支持操作后,如何在我的视图中增加 upvote 总数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 Thing 模型,它通过 UpVote 模型获得了很多赞成票.我希望能够通过 Ajax 从 things/show 创建一个新的 UpVote 对象,并在不刷新页面的情况下增加 upvote 总数.

通过 Ajax 创建新的 UpVote 记录有效,但是我无法增加视图中的 upvote 计数.

如何在成功创建赞成票后增加赞成票总数?

这是我迄今为止尝试过的:

views/things/show.html.erb

<div id="upvote"><%[email protected]_votes.count %>
<%= link_to "upvotething", upvote_thing_path(@thing.id), :remote =>真,:id =>"new_upvote_link", 方法: :post, :class =>btn btn-小"%>

views/things/create.js.erb

$('#new_up_vote').remove();$('#new_up_vote_link').show();$('#up_votes').append('<%= j render("up_vote", :up_vote =>@up_vote)%>');

views/things/upvote.js.erb

alert("这里");$('#up_votes').html('<%= @new_votes_count %>');

controllers/things_controller.rb

class ThingsController 

models/thing.rb

class 东西 

models/up_vote.rb

class UpVote 

application.js

//= 需要 jquery//= 需要 jquery_ujs//= 需要 jquery-ui//= 需要引导程序//= 需要涡轮链接//= 要求树

routes.rb

#...发布东西/upvote/:id"=>'things#upvote',如:'upvote_thing'资源:做的事情资源:up_votes结尾

application.js 头

<title>应用</title><%= stylesheet_link_tag "application", media: "all", "data-turbolinks-track" =>真%><%= javascript_include_tag "application", "data-turbolinks-track" =>真%><%= csrf_meta_tags %><%= stylesheet_link_tag "jquery-ui.min" %><%= javascript_include_tag "external/jquery/jquery" %><%= javascript_include_tag "jquery-ui.min" %>
解决方案

没有一个答案对我有用...这就是我最终做的:

things/show.hmtl.erb:

<%= thing.up_votes.count %>

<a id="<%= thing.name.downcase %>_link"><%= link_to "upvote", upvote_thing_path(:id => thing.id), remote: true, method: :得到%>/a><script type="text/javascript">$("#_link").click(function () {$.get(" thing.id) %>", function( data ) {$('#').html(data + '%');});});

controllers/things_controller.rb

def upvote@thing = Thing.find(params[:id])UpVote.create!(ip:request.remote_ip,消失:false,voteable_id:params[:id],voteable_type:'东西')重定向到@thing渲染文本:@thing.up_votes.count.to_s结尾

I have a Thing model that has many upvotes via an UpVote model. I want to be able to create a new UpVote object via Ajax from things/show and increment the upvote total without refreshing the page.

Creating a new UpVote record via Ajax works, however I cannot increment the upvote count in the view.

How can I increment the upvote totals upon successful creation of an upvote?

Here is what I have tried so far:


views/things/show.html.erb

<div id= "thing">

  <div id="upvote">
    <%= @thing.up_votes.count %>
  </div>

  <div id= "vote">
    <%= link_to "upvotething", upvote_thing_path(@thing.id), :remote => true, :id => "new_upvote_link", method: :post, :class => "btn btn-small" %>
  </div> 

</div>


views/things/create.js.erb

$('#new_up_vote').remove();
$('#new_up_vote_link').show();
$('#up_votes').append('<%= j render("up_vote", :up_vote => @up_vote)%>');


views/things/upvote.js.erb

alert("here");
$('#up_votes').html('<%= @new_votes_count %>');


controllers/things_controller.rb

class ThingsController < ApplicationController

  def show
    @thing = Thing.find(params[:id])
    @thing.up_votes.build
    @up_vote = UpVote.new
  end

  def upvote
    @thing = Thing.find(params[:id])
    UpVote.create!(ip: request.remote_ip, voteable_id: params[:id], voteable_type: 'Thing')
    respond_to do |format|
      if @up_vote.save
        @new_votes_count = @thing.up_votes.count
        format.html { redirect_to @thing, notice: 'Voted up' }
        format.json { render json: @up_vote, status: :created, location: @up_vote }
        format.js
      else
        @new_votes_count = @thing.up_votes.count
        format.html { redirect_to @thing, notice: 'Voted up failed' }
        format.json { render json: @up_vote.errors, status: :unprocessable_entity }
        format.js
      end
    end
  end
end

  private

    def thing_params
      params.require(:thing).permit(:name, :avatar, :email)
    end

end


models/thing.rb

class Thing < ActiveRecord::Base
  has_many :up_votes, as: :voteable
  # ...
end


models/up_vote.rb

class UpVote < ActiveRecord::Base
  belongs_to :voteable, polymorphic: true
end


application.js

//= require jquery
//= require jquery_ujs
//= require jquery-ui
//= require bootstrap
//= require turbolinks
//= require_tree


routes.rb

#...
post 'things/upvote/:id' => 'things#upvote', as: 'upvote_thing'
resources :things do
  resources :up_votes
end


application.js head

<head>
  <title>Application</title>
  <%= stylesheet_link_tag    "application", media: "all", "data-turbolinks-track" => true %>
  <%= javascript_include_tag "application", "data-turbolinks-track" => true %>
  <%= csrf_meta_tags %>
  <%= stylesheet_link_tag    "jquery-ui.min" %>
  <%= javascript_include_tag "external/jquery/jquery" %>
  <%= javascript_include_tag "jquery-ui.min" %>
</head>
解决方案

None of the answers worked for me...This is what I ended up doing:

things/show.hmtl.erb:

<div id="<%= thing.name %>"> <%= thing.up_votes.count %> </div>
<a id="<%= thing.name.downcase %>_link"><%= link_to "upvote", upvote_thing_path(:id => thing.id), remote: true, method: :get %></a>
<script type="text/javascript">
  $("#<%= thing.name.downcase %>_link").click(function () {
    $.get( "<%= upvote_thing_path(:id => thing.id) %>", function( data ) {
      $('#<%= thing.name %>').html(data + ' %');
    });
  });
</script>

controllers/things_controller.rb

def upvote
  @thing = Thing.find(params[:id])
  UpVote.create!(ip: request.remote_ip, vanishing: false, voteable_id: params[:id], voteable_type: 'Thing')
  redirect_to @thing
  render text: @thing.up_votes.count.to_s
end

这篇关于成功的 Ajax 请求以支持操作后,如何在我的视图中增加 upvote 总数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-24 09:19