Jquery 如何在Ajax请求upvote操作成功后在我的视图中增加upvote total
我有一个Jquery 如何在Ajax请求upvote操作成功后在我的视图中增加upvote total,jquery,ruby-on-rails,ruby,ajax,ruby-on-rails-4,Jquery,Ruby On Rails,Ruby,Ajax,Ruby On Rails 4,我有一个东西模型,它通过向上投票模型拥有许多向上投票。我希望能够通过Ajax从things/show创建一个新的UpVote对象,并在不刷新页面的情况下增加UpVote总数 通过ajaxworks创建一个新的UpVote记录,但是我不能增加视图中的UpVote计数 成功创建upvote后,如何增加upvote总数 以下是我迄今为止所尝试的: views/things/show.html.erb <div id= "thing"> <div id="upvote">
东西
模型,它通过向上投票
模型拥有许多向上投票。我希望能够通过Ajax从things/show
创建一个新的UpVote
对象,并在不刷新页面的情况下增加UpVote总数
通过ajaxworks创建一个新的UpVote
记录,但是我不能增加视图中的UpVote计数
成功创建upvote后,如何增加upvote总数
以下是我迄今为止所尝试的:
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>
$('#new_up_vote').remove();
$('#new_up_vote_link').show();
$('#up_votes').append('<%= j render("up_vote", :up_vote => @up_vote)%>');
alert("here");
$('#up_votes').html('<%= @new_votes_count %>');
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
class Thing < ActiveRecord::Base
has_many :up_votes, as: :voteable
# ...
end
class UpVote < ActiveRecord::Base
belongs_to :voteable, polymorphic: true
end
#...
post 'things/upvote/:id' => 'things#upvote', as: 'upvote_thing'
resources :things do
resources :up_votes
end
<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>
$('#up_votes').html('<%= @new_votes_count %>');
class ThingsController < ApplicationController
def upvote
@thing = Thing.find(params[:id])
@up_vote = UpVote.new(ip: request.remote_ip, votable: @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
var num = +$('#up_votes').text();
num += 1;
$('#up_votes').text(num);
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
routes.rb
<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>
$('#new_up_vote').remove();
$('#new_up_vote_link').show();
$('#up_votes').append('<%= j render("up_vote", :up_vote => @up_vote)%>');
alert("here");
$('#up_votes').html('<%= @new_votes_count %>');
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
class Thing < ActiveRecord::Base
has_many :up_votes, as: :voteable
# ...
end
class UpVote < ActiveRecord::Base
belongs_to :voteable, polymorphic: true
end
#...
post 'things/upvote/:id' => 'things#upvote', as: 'upvote_thing'
resources :things do
resources :up_votes
end
<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>
$('#up_votes').html('<%= @new_votes_count %>');
class ThingsController < ApplicationController
def upvote
@thing = Thing.find(params[:id])
@up_vote = UpVote.new(ip: request.remote_ip, votable: @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
var num = +$('#up_votes').text();
num += 1;
$('#up_votes').text(num);
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
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>
应用
正确%>
正确%>
这是你应该做的
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>
$('#new_up_vote').remove();
$('#new_up_vote_link').show();
$('#up_votes').append('<%= j render("up_vote", :up_vote => @up_vote)%>');
alert("here");
$('#up_votes').html('<%= @new_votes_count %>');
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
class Thing < ActiveRecord::Base
has_many :up_votes, as: :voteable
# ...
end
class UpVote < ActiveRecord::Base
belongs_to :voteable, polymorphic: true
end
#...
post 'things/upvote/:id' => 'things#upvote', as: 'upvote_thing'
resources :things do
resources :up_votes
end
<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>
$('#up_votes').html('<%= @new_votes_count %>');
class ThingsController < ApplicationController
def upvote
@thing = Thing.find(params[:id])
@up_vote = UpVote.new(ip: request.remote_ip, votable: @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
var num = +$('#up_votes').text();
num += 1;
$('#up_votes').text(num);
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
true,:id=>new\u upvote\u link,method::post,:class=>“btn btn small”%>
views/things/upvote.js.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>
$('#new_up_vote').remove();
$('#new_up_vote_link').show();
$('#up_votes').append('<%= j render("up_vote", :up_vote => @up_vote)%>');
alert("here");
$('#up_votes').html('<%= @new_votes_count %>');
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
class Thing < ActiveRecord::Base
has_many :up_votes, as: :voteable
# ...
end
class UpVote < ActiveRecord::Base
belongs_to :voteable, polymorphic: true
end
#...
post 'things/upvote/:id' => 'things#upvote', as: 'upvote_thing'
resources :things do
resources :up_votes
end
<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>
$('#up_votes').html('<%= @new_votes_count %>');
class ThingsController < ApplicationController
def upvote
@thing = Thing.find(params[:id])
@up_vote = UpVote.new(ip: request.remote_ip, votable: @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
var num = +$('#up_votes').text();
num += 1;
$('#up_votes').text(num);
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
$('#向上投票').html('';
controller/things\u controller.rb
<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>
$('#new_up_vote').remove();
$('#new_up_vote_link').show();
$('#up_votes').append('<%= j render("up_vote", :up_vote => @up_vote)%>');
alert("here");
$('#up_votes').html('<%= @new_votes_count %>');
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
class Thing < ActiveRecord::Base
has_many :up_votes, as: :voteable
# ...
end
class UpVote < ActiveRecord::Base
belongs_to :voteable, polymorphic: true
end
#...
post 'things/upvote/:id' => 'things#upvote', as: 'upvote_thing'
resources :things do
resources :up_votes
end
<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>
$('#up_votes').html('<%= @new_votes_count %>');
class ThingsController < ApplicationController
def upvote
@thing = Thing.find(params[:id])
@up_vote = UpVote.new(ip: request.remote_ip, votable: @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
var num = +$('#up_votes').text();
num += 1;
$('#up_votes').text(num);
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
类内容控制器
如果您还需要其他内容,请添加注释确保您的
应用程序.js中包含以下内容:
确保您使用Jquery,我只会使用
Update.js.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>
$('#new_up_vote').remove();
$('#new_up_vote_link').show();
$('#up_votes').append('<%= j render("up_vote", :up_vote => @up_vote)%>');
alert("here");
$('#up_votes').html('<%= @new_votes_count %>');
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
class Thing < ActiveRecord::Base
has_many :up_votes, as: :voteable
# ...
end
class UpVote < ActiveRecord::Base
belongs_to :voteable, polymorphic: true
end
#...
post 'things/upvote/:id' => 'things#upvote', as: 'upvote_thing'
resources :things do
resources :up_votes
end
<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>
$('#up_votes').html('<%= @new_votes_count %>');
class ThingsController < ApplicationController
def upvote
@thing = Thing.find(params[:id])
@up_vote = UpVote.new(ip: request.remote_ip, votable: @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
var num = +$('#up_votes').text();
num += 1;
$('#up_votes').text(num);
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
$in var num=+$(“#向上_投票”)前面的+。text();将字符串转换为yout increment的整数
您还可以使用rails API提供的rails increment()命令执行以下操作:
(在控制器中)
将respond\u to:html,:js
添加到控制器顶部
def upvote
@thing = Thing.find(params[:id])
@up_vote = @thing.up_votes.build(ip: request.remote_ip)
if @up_vote.save
respond_with @new_votes_count do |format|
format.html { redirect_to @thing, notice: 'Voted up' }
format.js {
@new_votes_count = @thing.up_votes.count
}
end
else
render :show
end
end
没有一个答案对我有用…这就是我最后做的:
事物/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>
$('upvote')。追加('upvote)%>)代码>@Substantial我也认为这很奇怪,但这是我在上一篇教程中使用的语法。这是一个相当严重的错误;缺少一整段代码。它看起来像是对助手方法的调用。难怪它不起作用。你有找到它的链接吗?这篇博文搞砸了。下面是:$('#products').append('@product)%>')代码>我给博客所有者留下了一条评论。下次相信你的眼睛。是的。谢谢你的回答,但我键入的代码与写的完全一样,@thing.up\u voces.count仍然没有增加。如果服务器日志有任何错误,你能检查一下吗,因为这似乎是一个有效的解决方案。@MohamedDia27不,没有错误。远程创建新的UpVote,但thing.up_votes.count未刷新。请尝试添加警报(“此处”)
在upvote.js.erb
中,为了确保它成功到达视图(在文件顶部),我编辑了我的upvote.js.erb
,以确保这正是您想要的,但如果是这样,它什么也没有做。我将它与我的路线一起发布在上面。它包括jquery和jquery_ujs。你是否在你的application.html中包含true%>
?是的,我也发布了我的应用程序头。只是为了测试,将你对js的响应修改为format.js{render:layout=>false}
。我包含了,它没有改变任何东西。