我正在尝试创建一个电子邮件,当管理员点击该用户的活动帐户时会发送该电子邮件
用户has_one:帐户和帐户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
在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 => "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>
答案 0 :(得分: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))
user_account = account.find_by_user_id(@user)
AccountMailer.activate_account(@user,user_account,@account.user.mail).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,account,to)
@users = user
@account = account
mail(:to => to, :subject => "Activation Account", :from=>"kapanjadi@gmail.com")
end
end
以上内容肯定会有用。
答案 1 :(得分:0)
为什么一切都是复数(@accounts
和@users
)?
def activate
@account = Account.find(params[:id])
redirect_to accounts_path and return if @account.nil? # handles bad `params[:id]`
if @account.update_attributes(approved: true) && @account.user.update_attributes(approved: true)
AccountMailer.activate_account(@account.user).deliver
redirect_to accounts_path and return
end
redirect_to accounts_path
end