ruby-on-rails - 创建操作后,Merit 不会为用户添加积分

标签 ruby-on-rails ruby ruby-on-rails-4 merit-gem

当用户创建“解决方案”(这是对微博的一种“回答”)时,我使用此说明来简单地添加分数。我已将 has_merit 行添加到 user.rb(用户模型)。

我想在展示 View 中显示该操作获得的用户积分。 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 创建一个解决方案时(已经将 user_id 索引和标识符保存到解决方案中),这就是我的 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

最佳答案

我在安装 merit gem 时遇到了迁移问题。

关于ruby-on-rails - 创建操作后,Merit 不会为用户添加积分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31758478/

相关文章:

ruby-on-rails - 为 ActiveAdmin Controller 设置过滤器 before_action

html - 如何将 html.slim 转换为 html.erb?

ruby - 使用 Nokogiri 解析大型 XML

ruby-on-rails - rails : Set a common or global instance variable across several controller actions

html - 如何使用 gem htmltoword 在 Rails 中放置页眉和页脚?

html - 使用 Nokogiri 替换所有出现的 CSS 类

ruby - 在初始化时使用 attr_accessor 设置 ruby​​ 2.0 关键字参数

ruby-on-rails - 路由到新操作 (Rails)

jquery - Rails3 中通过 AJAX 请求加载页面内容的最佳实践?

ruby-on-rails - 如何配置 Rails 应用程序 (redmine) 以在 Windows 上作为服务运行?