4

Just got the Merit gem up and running with my rails 4 app.

So far, a user receives the first badge upon creating a subarticle.

The second badge I am trying to award is when that subarticle actually gets approved, which can only be done by an admin. I have tried using subarticle#update as the action, but it is not awarding the user the badge. Maybe it has to do with the admin updating the subarticle from the admin panel (rails admin gem)?

Here is my badge in the initializer:

Merit::Badge.create!(
  id: 2,
  name: "Serious Writer",
  description: "Things are gettin' serious! You have successfully written an approved article."
  )

and here are the rules:

grant_on ['subarticles#update','subarticles#destroy'], badge_id: 2, badge:'serious writer', to: :user, temporary: true do |subarticle|
  subarticle.user.subarticles.approved.count >= 1
end

Any suggestions? Still new to this gem so maybe its a simple mistake.

4

1 回答 1

2

您批准文章的设置是什么?你可以在你的控制器中做到这一点,但我更愿意让它们保持苗条,并在你的胖模型中完成逻辑。

请注意,还有其他方法可以做到这一点,但这是我过去所做的。

class Subarticles < ActiveRecord::Base
  ...
  after_update :provide_badge_when_approved

  private

  def provide_badge_when_approved
    self.user.add_badge(2) if (self.approved_changed? && self.approved == true)
  end
  ...
end
于 2015-07-19T20:36:02.510 回答