2013-09-16 107 views
0

在我們的Rails應用程序中,我們希望在啓動過程中加載郵件服務器配置。下面是我們所擁有的配置/環境/ development.rb:如何在rails 3.2啓動時加載郵件服務配置?

#Action Mailer 
    config.action_mailer.smtp_settings = eval(Authentify::AuthentifyUtility.find_config_const('development_smtp_setting')) 

Authentify::AuthentifyUtility.find_config_const('development_smtp_setting')將返回:

{ 
    :address    => "mymail.com", 
    :port     => 587,  
    :user_name   => "smtp_login_name", 
    :password    => "password", 
    :authentication  => :plain, 
    :enable_starttls_auto => false 
    } 

然而rails server拋出錯誤:

activerecord-3.2.12/lib/active_record/connection_adapters/abstract/connection_pool.rb:410:in `retrieve_connection' 
: ActiveRecord::ConnectionNotEstablished (ActiveRecord::ConnectionNotEstablished) 

看來,在加載時間內沒有數據庫連接。加載郵件服務器配置的正確方法是什麼?謝謝。

回答

2

你需要從數據庫中加載它嗎?通常你可以從文件或環境變量中加載。也看看config.after_initializehttp://guides.rubyonrails.org/configuring.html#rails-general-configuration

+0

是的,郵件配置參數需要從數據庫表加載。 – user938363

+0

config.after_initialize起作用。感謝:config.after_initialize do config.action_mailer.smtp_settings = eval(Authentify :: AuthentifyUtility.find_config_const('development_smtp_setting')) end – user938363

+0

太棒了! FWIW'eval'是一個主要的安全風險! http://stackoverflow.com/questions/637421/is-eval-supposed-to-be-nasty你最好解析字符串,或將其存儲爲JSON或YAML並使用內置的解析器。這篇文章是一個很好的開始:http://stackoverflow.com/questions/1667630/how-do-i-convert-a-string-object-into-a-hash-object這將更安全,可能更快太!你甚至可以有軌道做你的編碼和解碼:http://stackoverflow.com/questions/6694432/using-rails-serialize-to-save-hash-to-database – Josh

相關問題