我正在尝试创建一封电子邮件,当管理员单击用户的活动帐户时发送该电子邮件
用户 has_one :account 和 account belongs_to :user
在用户.rb
devise :database_authenticatable, :registerable, :Trackable, :rememberable, :recoverable
attr_accessible :account, :email, :password, :password_confirmation, :account_attributes, :approved
has_one :account
end
在帐户.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 => "kapanjadi@gmail.com"
def activate_account(user)
@users = user
@account = account.find_by_user_id(user)
mail(:to => user.email, :subject => "Activation Account", :from => "kapanjadi@gmail.com")
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 => "kapanjadi@gmail.com",
:password => "password",
:authentication => "plain",
:enable_starttls_auto => true
}
但是一封电子邮件没有发送给电子邮件用户,并且没有错误..这些没有发生在任何其他操作中。
关于我做错了什么的任何建议?
更新 1
在accounts_controller.rb
def 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 => "fauzieuy@gmail.com"
def activate_account(user,account)
@account = account
@accounts = Account.find_by_user_id(user)
mail(:to => user.email, :subject => "Activation", :from => "kapanjadi@gmail.com")
end
end
错误
undefined method `email' for #<ActiveRecord::Relation:0x3e7c720>