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
in account_mailer.rb
The above will work for sure.