Ich habe eine Liste von Beiträgen und alle von ihnen können votable sein. Ich kann die Anzahl der Stimmen für jeden Beitrag zählen, aber wie kann ich die Anzahl für jeden von ihnen zählen? Ich bin mit dem Juwel acts_as_votable für das WahlsystemSchienen: Anzahl der Gesamtstimmen aus einer Liste von Beiträgen (acts_as_votable)
ich die Anzahl der Beiträge wie diese zählen: <%= performance_indicator.improvement_actions.count %>
das ist mein „Beiträge“ Controller:
class ImprovementActionsController < ApplicationController
before_action :set_improvement_action, only: [:show, :edit, :update, :destroy, :upvote, :downvote]
before_action :authenticate_user!, except: [:index, :show]
# GET /improvement_actions
# GET /improvement_actions.json
def index
end
# GET /improvement_actions/1
# GET /improvement_actions/1.json
def show
end
# GET /improvement_actions/new
def new
@performance_indicator = PerformanceIndicator.find(params[:performance_indicator_id])
@improvement_action = ImprovementAction.new
@comment = @improvement_action.comments.new
end
# GET /improvement_actions/1/edit
def edit
end
# POST /improvement_actions
# POST /improvement_actions.json
def create
@performance_indicator = PerformanceIndicator.find(params[:performance_indicator_id])
@improvement_action = @performance_indicator.improvement_actions.create(params[:improvement_action].permit(:description))
@improvement_action.user_id = current_user.id if current_user
@improvement_action.save
respond_to do |format|
if @improvement_action.save
format.html { redirect_to @performance_indicator }
format.json { render :show, status: :created, location: @improvement_action }
else
format.html { render :new }
format.json { render json: @improvement_action.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /improvement_actions/1
# PATCH/PUT /improvement_actions/1.json
def update
respond_to do |format|
if @improvement_action.update(improvement_action_params)
format.html { redirect_to performance_indicator_path(@improvement_action.performance_indicator), notice: 'Improvement action was successfully updated.' }
format.json { render :show, status: :ok, location: @performance_indicator }
else
format.html { render :edit }
format.json { render json: @improvement_action.errors, status: :unprocessable_entity }
end
end
end
def destroy
@improvement_action.destroy
respond_to do |format|
format.html { redirect_to performance_indicator_path(@improvement_action.performance_indicator), notice: 'Improvement action was successfully deleted.' }
format.json { head :no_content }
end
end
#upvote_from user
#downvote_from user
def upvote
@improvement_action.upvote_from current_user
# respond_to do |format|
# format.html { redirect_to :back }
# format.js { render layout: false }
# end
redirect_to :back
end
def downvote
@improvement_action.downvote_from current_user
redirect_to :back
##respond_to do |format|
# format.html { redirect_to :back }
# format.js { render layout: false }
# end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_improvement_action
@improvement_action = ImprovementAction.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def improvement_action_params
params.require(:improvement_action).permit(:description, :upvote, :downvote, :score, :active)
end
end
Und ich möchte hier setzen der Zähler:
<% @performance_indicators.each do |performance_indicator| %>
<p> Number of votes </p>
<% end %>
http://apidock.com/ruby/Enumerable/inject –