2

我正在使用 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
4

2 回答 2

3

根据config/environments您希望完整的电子邮件功能在什么环境下工作,设置 ActionMailer 配置是先决条件。例如,如果您希望在开发中发送电子邮件,这些应该出现在development.rbproduction.rb用于生产)中:

  # change to true to allow email to be sent during development
  config.action_mailer.perform_deliveries = false

  config.action_mailer.smtp_settings = {
    address: "smtp.gmail.com",
    port: 587,
    domain: "example.com",
    authentication: "plain",
    enable_starttls_auto: true,
    user_name: ENV["GMAIL_USERNAME"], # you can use ordinary gmail username here
    password: ENV["GMAIL_PASSWORD"]   # you can use your gmail password here, but don't push the changes
  }
于 2012-09-02T06:37:53.950 回答
1

设计邀请文档说明您需要

devise :database_authenticatable, :confirmable, :invitable

让它工作。您已经放弃了:confirmable某个地方,这是设计的一部分,它会为还没有令牌的用户自动生成该令牌。模型中的那一行应该是:

devise :database_authenticatable, :recoverable, :rememberable, :trackable, :confirmable, :invitable
于 2012-06-22T23:11:14.683 回答