0

我有一个使用 devise、rails 4 和 ruby​​ 2.1.0 的 rails 应用程序。Devise 正在使用user带有config.scoped_views = true. 我已经按照这个 wiki使用 devise 设置优点。当用户注册时,徽章不会添加到数据库中 - 如果我在操作中手动添加徽章,它确实可以工作,create并且current_user.add_badge('1')我可以在视图中显示徽章。我已经在下面发布了相关代码,如果有人能告诉我我错过了什么?Merit 提供了很棒的功能,我真的很想让它工作 - 谢谢!

app/controllers/users/registrations_controller.rb

class Users::RegistrationsController < Devise::RegistrationsController
  def create
    @user = build_resource # Needed for Merit
    super
  end

  def update
    @user = resource # Needed for Merit
    super
  end
end

app/models/user.rb

class User < ActiveRecord::Base
  has_merit

  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable
end

config/routes.rb

Rails.application.routes.draw do
  devise_for :users, controllers: { registrations: 'users/registrations' }
end

app/models/merit/badge_rules.rb

module Merit
  class BadgeRules
    include Merit::BadgeRulesMethods

    def initialize
      grant_on 'registrations#create', badge: 'just-registered', model_name: 'User'
    end
  end
end

config/initializers/merit.rb

Merit.setup do |config|
  config.checks_on_each_request = true
end

Badge.create!(
  id: 1,
  name: 'just-registered'
)
4

0 回答 0