I've used this instructions for simply add score when a user creates a "solucion" (which is a kind of "answer" to a micropost). I have added the has_merit
line to user.rb (user model).
I want to display the user points earned for that action at the show view. show.html.erb (for solucion):
<h2><span class="red"><%= current_user.points %></span><br>Points</br></h2>
It displays 0 points...
point_rules.rb:
module Merit
class PointRules
include Merit::PointRulesMethods
def initialize
score 5, on: 'solucions#create'
end
end
end
When I create a solucion with the current_user (already saving the user_id index and identifier to solucion), This is what my rails server output shows...
Direct link to github gist:
https://gist.github.com/roadev/7b34fd67ab93c979fa48
Embed:
<script src="https://gist.github.com/roadev/7b34fd67ab93c979fa48.js"></script>
EDIT:
solucions_micropost.rb
class SolucionsController < ApplicationController
before_action :set_solucion, only: [:show, :edit, :update, :destroy]
def index
@solucions = Solucion.all
end
def show
end
def new
@solucion = current_user.solucions.build
end
def edit
end
def create
@solucion = current_user.solucions.build(solucion_params)
respond_to do |format|
if @solucion.save
format.html { redirect_to @solucion, notice: 'Solucion was successfully created.' }
format.json { render action: 'show', status: :created, location: @solucion }
else
format.html { render action: 'new' }
format.json { render json: @solucion.errors, status: :unprocessable_entity }
end
end
end
def update
respond_to do |format|
if @solucion.update(solucion_params)
format.html { redirect_to @solucion, notice: 'Solucion was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: 'edit' }
format.json { render json: @solucion.errors, status: :unprocessable_entity }
end
end
end
def destroy
@solucion.destroy
respond_to do |format|
format.html { redirect_to solucions_url }
format.json { head :no_content }
end
end
private
def set_solucion
@solucion = Solucion.find(params[:id])
end
def current_micropost
@solucion = microposts.find_by(id: params[:id])
end
def solucion_params
params.require(:solucion).permit(:solucion, :image, :micropost_id)
end
end
user.rb:
class User < ActiveRecord::Base
has_many :dreams
has_many :microposts
has_many :solucions
has_merit
end
I had a problem with a migration when I installed the merit gem.