1

我正在尝试创建一封电子邮件,当管理员单击用户的活动帐户时发送该电子邮件

用户 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>
4

2 回答 2

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


以上肯定会奏效。

于 2012-12-11T05:12:08.343 回答
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
于 2012-12-11T04:20:46.833 回答