我正在使用 devise 和devise_invitable
gem 'devise', '>= 2.0.0'
gem 'devise_invitable', '~> 1.0.0'
我在网站的管理部分有一个链接,该链接会自动向用户发送邀请电子邮件
= link_to 'Send Invitation', invite_user_path(@user), :remote => true, :title => "Sends an email directly to the user"
def invite
@user = User.find(params[:id])
User.invite!(:email => @user.email)
flash.now[:success] = "Invitation email has been sent"
respond_to do |format|
format.js
end
end
这会向数据库中 user.invitation_token 不为 NULL 的用户发送电子邮件,但会跳过向其他用户发送电子邮件....为什么会这样,我该如何纠正。同样在devise_invitable的文档中,这是他们所说的调用邀请的方式!
User.invite!(:email => "new_user@example.com", :name => "John Doe")
但是当我这样做时,他们说我不能批量分配属性名称
任何帮助将不胜感激
我注意到如果用户invitation_token 字段中有任何内容,电子邮件将会发出,但我最初如何创建它
更新...这是我的整个用户模型
class User < ActiveRecord::Base
delegate :can?, :cannot?, :to => :ability
has_many :roles, :through => :role_users
has_many :role_users
has_many :notifications, :through => :subscriptions
has_many :subscriptions
has_many :companies, :through => :positions
has_many :positions
has_many :notification_histories
has_many :sites, :through => :site_users
has_many :site_users
scope :admins, joins(:roles).where("roles.name = 'SuperAdmin' or roles.name = 'SubAdmin'")
scope :regular, joins(:companies).where('positions.regular_user = 1').group('users.id')
scope :employee, joins(:companies).where('positions.regular_user = 0').group('users.id')
scope :current, :conditions => { :active => true }, :order => 'LOWER(first_name), LOWER(last_name) ASC'
default_scope :order => 'LOWER(first_name) ASC'
has_many :feedbacks do
def for_playlist(id)
find_or_create_by_playlist_id(id)
end
end
def name
"#{self.first_name} #{self.last_name}"
end
has_many :ratings, :through => :feedbacks do
def for_playlist(id)
where('feedbacks.playlist_id = ?', id)
end
end
Role::TYPES.each do |role|
define_method(role + '?') do
self.has_role?(role)
end
end
def has_role?(role_name)
role_name = role_name.name if role_name.is_a?(Role)
self.roles.where(:name => role_name.to_s).exists?
end
accepts_nested_attributes_for :roles, :notifications, :allow_destroy => true
def company
self.companies.first
end
def site
self.sites.first
end
validates :first_name, :presence => true
validates :last_name, :presence => true
validates :email, :presence => true
validates_format_of :email, :with => /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\Z/i, :on => :create, :if => :email_present?
validates_uniqueness_of :email
validates_format_of :phone_number, :with => /^\(?([0-9]{3})\)?[-. ]?([0-9]{3})[-. ]?([0-9]{4})$/, :allow_nil => true
validates_format_of :password,
:with => /^.*(?=.{6,})(?=.*[a-z])(?=.*[A-Z])(?=.*[\d\W]).*$/,
:message => "must be at least 6 characters, have one number and one capital letter",
:if => Proc.new { |user| !user.password.blank? }
before_validation :clear_empty_attrs
before_validation :clean_data
# Include default devise modules
devise :database_authenticatable, :recoverable, :rememberable, :trackable, :invitable
# Setup accessible (or protected) attributes for devise support
attr_accessible :email, :password, :password_confirmation, :remember_me, :active,
:company_id, :first_name, :last_name, :phone_number, :role_ids, :notification_ids, :name
def role?(role)
return !!self.roles.find_by_name(role.to_s.camelize)
end
def ability
@ability ||= Ability.new(self)
end
def name
"#{first_name} #{last_name}"
end
def list_companies
companies.map(&:name).try(:join, ", ").try(:titlecase)
end
def email_present?
!self.email.blank?
end
protected
def clear_empty_attrs
@attributes.each do |key,value|
self[key] = nil if value.blank?
end
end
def clean_data
self.email.downcase! unless self.email.nil?
end
end