成功的Ajax请求成功后,如何在我的视图中增加upvote total
我有一个Thing
模型,通过UpVote
模型有许多upvotes。 我希望能够通过things/show
Ajax创建一个新的UpVote
对象,并在不刷新页面的情况下增加upvote总数。
通过Ajax创建一个新的UpVote
记录是有效的,但是我不能在视图中增加upvote计数。
如何在成功创建upvote时增加upvote总数?
这是我到目前为止所尝试的:
意见/事/ show.html.erb
true, :id => "new_upvote_link", method: :post, :class => "btn btn-small" %>
意见/事/ create.js.erb
$('#new_up_vote').remove(); $('#new_up_vote_link').show(); $('#up_votes').append(' @up_vote)%>');
意见/事/ upvote.js.erb
alert("here"); $('#up_votes').html('');
控制器/ 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
车型/ thing.rb
class Thing < ActiveRecord::Base has_many :up_votes, as: :voteable # ... end
车型/ 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头
Application true %> true %>
这是你应该怎么做的。
意见/事/ show.html.erb
<%= @thing.up_votes.count %> <%= link_to "upvotething", upvote_thing_path(@thing.id), :remote => true, :id => "new_upvote_link", method: :post, :class => "btn btn-small" %>
意见/事/ upvote.js.erb
$('#up_votes').html('<%= @new_votes_count %>');
控制器/ things_controller.rb
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
如果您还需要其他内容,请添加评论
确保application.js
包含以下内容:
//= require jquery //= require jquery_ujs
确保你使用Jquery,我只是用它增加它
Update.js.erb
var num = +$('#up_votes').text(); num += 1; $('#up_votes').text(num);
$ in in num num = + $(’#up_votes’)。text(); 将字符串转换为整数,以便增加。
您还可以使用rails API提供的rails increment()命令来执行以下操作:(在您的控制器中)
@new_votes_count.increment!(:up_votes)
将respond_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:
<%= thing.up_votes.count %> <%= link_to "upvote", upvote_thing_path(:id => thing.id), remote: true, method: :get %>
控制器/ 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