我試圖創造出發送當管理員點擊活躍賬戶用戶的ActionMailer爲Appove帳戶 - 電子郵件不發送
用戶HAS_ONE電子郵件:戶名及賬號belongs_to的:用戶
用戶。 RB
devise :database_authenticatable, :registerable, :Trackable, :rememberable, :recoverable
attr_accessible :account, :email, :password, :password_confirmation, :account_attributes, :approved
has_one :account
end
在account.rb
class Account < ActiveRecord::Base
attr_accessible :address, :approved, :name
belongs_to :user
end
在accounts_controller.rb
def activate
@accounts = Account.find(params[:id])
@users = User.where(:id => @accounts.id)
if (@accounts.update_attributes(approved: true)) && (@users.update_all(approved: true))
AccountMailer.activate_account(@users).deliver
redirect_to accounts_path
else
redirect_to accounts_path
end
end
在account_mailer.rb
class AccountMailer < ActionMailer::Base
default :from => "[email protected]"
def activate_account(user)
@users = user
@account = account.find_by_user_id(user)
mail(:to => user.email, :subject => "Activation Account", :from => "[email protected]")
end
end
在account_mailer
/activate_account.text.erb
congratulation, your account is active now
setup_mailer.rb
ActionMailer::Base.smtp_settings = {
:address => "smtp.gmail.com",
:port => 587,
:domain => "localhost:3000",
:user_name => "[email protected]",
:password => "password",
:authentication => "plain",
:enable_starttls_auto => true
}
但郵件無法發送電子郵件用戶,並沒有錯誤.. 這些沒有發生任何其他行動。
任何關於我在做什麼的錯誤?
UPDATE 1
在accounts_controller.rbdef activate
@account = Account.find(params[:id])
@user = User.where(:id => @account.id)
if (@account.update_attributes(approved: true)) && (@user.update_all(approved: true))
AccountMailer.activate_account(@user,@account).deliver
redirect_to accounts_path
else
redirect_to accounts_path
end
end
在account_mailer.rb
class AccountMailer < ActionMailer::Base
default :from => "[email protected]"
def activate_account(user,account)
@account = account
@accounts = Account.find_by_user_id(user)
mail(:to => user.email, :subject => "Activation", :from => "[email protected]")
end
end
錯誤
undefined method `email' for #<ActiveRecord::Relation:0x3e7c720>
我以前遇到過這個問題,Gmail不理會電子郵件。你無能爲力。我建議使用一個替代SMTP服務器,如sendgrid.com –
不,這些沒有發生任何其他行爲,smtp gmail正在爲其他行動工作。 我認爲控制器或郵件程序存在錯誤,但我不知道錯誤的位置。 – GeekToL
@users是accounts_controller.rb中的一個列表,而不僅僅是一行。使用「查找」而不是「where」 –