gpt4 book ai didi

ruby-on-rails - 为什么设计邀请不发送一些电子邮件

转载 作者:数据小太阳 更新时间:2023-10-29 08:39:55 25 4
gpt4 key购买 nike

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

最佳答案

config/environments 下,根据您希望完整电子邮件功能运行的环境,设置 ActionMailer 配置是先决条件。例如,如果您希望在开发中发送电子邮件,这些应该存在于 development.rb(production.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
}

关于ruby-on-rails - 为什么设计邀请不发送一些电子邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11164841/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com