【发布时间】:2015-08-01 05:11:48
【问题描述】:
当用户创建“解决方案”(这是对微博的一种“答案”)时,我已使用此说明简单地添加分数。我已将 has_merit 行添加到 user.rb(用户模型)。
我想在显示视图中显示该操作获得的用户积分。 show.html.erb(用于解决方案):
<h2><span class="red"><%= current_user.points %></span><br>Points</br></h2>
显示0分...
point_rules.rb:
module Merit
class PointRules
include Merit::PointRulesMethods
def initialize
score 5, on: 'solucions#create'
end
end
end
当我使用 current_user 创建一个 solucion(已经将 user_id 索引和标识符保存到 solucion)时,这就是我的 rails 服务器输出显示的内容...
直接链接到 github gist:
https://gist.github.com/roadev/7b34fd67ab93c979fa48
嵌入:
<script src="https://gist.github.com/roadev/7b34fd67ab93c979fa48.js"></script>
编辑:
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
用户.rb:
class User < ActiveRecord::Base
has_many :dreams
has_many :microposts
has_many :solucions
has_merit
end
【问题讨论】:
-
没有看到更多代码(控制器、测试等),我无法提供太多帮助。引起我注意的一件事:您的意思是“解决方案”吗?
-
应该是“解决方案”,但是几周前生成该模型的开发人员(讲西班牙语的人)有问题......无论如何,我要发布控制器代码。
标签: ruby-on-rails ruby ruby-on-rails-4 merit-gem