我有一个使用devise,rails 4和ruby 2.1.0的rails应用程序。设计使用user
模型和config.scoped_views = true
。我已经按照this wiki使用设计来设置优点。当用户注册徽章时没有添加到数据库中 - 如果我使用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'
)