2012-12-11 44 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

相關問題