I'm trying to create an email which is sent when admin click to active account for user
user has_one :account and account belongs_to :user
in user.rb
devise :database_authenticatable, :registerable, :Trackable, :rememberable, :recoverable
attr_accessible :account, :email, :password, :password_confirmation, :account_attributes, :approved
has_one :account
end
in account.rb
class Account < ActiveRecord::Base
attr_accessible :address, :approved, :name
belongs_to :user
end
in 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
in 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
in 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
}
but an email not send to email user, and no error.. these did not happened to any other action.
Any suggestions as to what I am doing wrong?
UPDATE 1
in 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
in 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
error
undefined method `email' for #<ActiveRecord::Relation:0x3e7c720>
in 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
in 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
The above will work for sure.
Why is everything plural ( @accounts
and @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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.