我正在使用devise_invitable来允许用户互相邀请。我想在创建邀请或接受邀请时为用户设置值。一种方法就在这里
Using devise_invitable for adding Users to a Group in Ruby on Rails?
但这似乎有点矫枉过正。回调看起来像一个完美的解决方案,但在我的Rspec测试期间似乎没有触发。
以下是用户模型:
class User < ActiveRecord::Base
belongs_to :company
rolify
# Include default devise modules. Others available are:
# :token_authenticatable, :confirmable,
# :lockable, :timeoutable and :omniauthable
devise :invitable, :database_authenticatable, :registerable, :confirmable,
:recoverable, :rememberable, :trackable, :validatable
# Setup accessible (or protected) attributes for your model
attr_accessible :role_ids, :as => :admin
attr_accessible :name, :email, :password, :password_confirmation, :remember_me, :company
validates :company, :presence => true
after_invitation_accepted :email_invited_by
before_invitation_accepted :email_invited_by
private
def email_invited_by
# This is NEVER executed during tests, even when an invite is successfully accepted
puts "Callback worked"
end
end
任何关于在哪里寻找的线索都将受到赞赏。我发现devise_invitable文档有点不透明。
谢谢!
答案 0 :(得分:0)
company
的存在,我认为这可能导致验证错误。
如果是这种情况,您可以添加这样的条件
validates :company, :presence => true, :if => Proc.new { self.invitation_token.nil? }
所以它不会导致验证错误并触发回调。
答案 1 :(得分:0)
对于那些仍在寻找答案的人来说,这对我有用。
问题不在于验证,因为在devise_invitable
中有一个配置要在邀请时进行验证,它默认为false
:
# Flag that force a record to be valid before being actually invited
# Default: false
# config.validate_on_invite = true
所以我的解决方案是使用devise_invitable
提供的回调:
after_invitation_accepted :create_profile
请注意,这需要低于devise :invitable