为什么设计可以不发送一些电子邮件

时间:2012-06-22 22:15:55

标签: ruby-on-rails ruby 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

这会向在db中使用user.invitation_token not 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

2 个答案:

答案 0 :(得分: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
  }

答案 1 :(得分:1)

设计邀请文档说明您需要

devise :database_authenticatable, :confirmable, :invitable

让它发挥作用。你已经在某个地方删除了:confirmable,这是设备的一部分,为那些还没有的用户自动生成该令牌。模型中的那一行应为:

devise :database_authenticatable, :recoverable, :rememberable, :trackable, :confirmable, :invitable