2012-12-11 43 views
1

我试图创造出发送当管理员点击活跃账户用户的ActionMailer为Appove帐户 - 电子邮件不发送

用户HAS_ONE电子邮件:户名及账号belongs_to的:用户

用户

。 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 => "[email protected]" 

    def activate_account(user) 
    @users = user 
     @account = account.find_by_user_id(user) 
    mail(:to => user.email, :subject => "Activation Account", :from => "[email protected]") 
    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   => "[email protected]", 
    :password    => "password", 
    :authentication  => "plain", 
    :enable_starttls_auto => true 
} 

但邮件无法发送电子邮件用户,并没有错误.. 这些没有发生任何其他行动。

任何关于我在做什么的错误?

UPDATE 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 => "[email protected]" 

    def activate_account(user,account) 
    @account = account 
    @accounts = Account.find_by_user_id(user) 
    mail(:to => user.email, :subject => "Activation", :from => "[email protected]") 
    end 

end 

错误

undefined method `email' for #<ActiveRecord::Relation:0x3e7c720> 
+0

我以前遇到过这个问题,Gmail不理会电子邮件。你无能为力。我建议使用一个替代SMTP服务器,如sendgrid.com –

+0

不,这些没有发生任何其他行为,smtp gmail正在为其他行动工作。 我认为控制器或邮件程序存在错误,但我不知道错误的位置。 – GeekToL

+0

@users是accounts_controller.rb中的一个列表,而不仅仅是一行。使用“查找”而不是“where” –

回答

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 => "[email protected]" 

    def activate_account(user,account,to) 
    @users = user 
    @account = account 
    mail(:to => to, :subject => "Activation Account", :from=>"[email protected]") 
    end 
end 


以上将肯定工作。

+0

quetion更新 – GeekToL

+0

谢谢。但我编辑了你的答案 – GeekToL

0

W¯¯ hy是一切复数@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 
+0

未定义的方法'用户'为零:NilClass – GeekToL

+0

我只是要提供你所提供的;你说'Account'' belongs_to'是一个'User',所以'@ account.user'应该是一个有效的关系,只要'@ account'赋值是有效的(即你提供了一个有效的':id''通过'params [:id]'为具有相关'用户'的'账户')。当提供错误的':id'时,您可以期望'@ account'为'nil'。 – deefour

+0

我为'@ account'为'nil'添加了一个条件 – deefour

相关问题