Load rails app config from database - ruby-on-rails

I'd like to load some config settings from the database when my Rails (3.2.13) app starts:
class MyApp < Rails::Application
#...normal config here...
config.action_mailer.raise_delivery_errors = true
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
:address => SystemSetting.system_smtp_host,
:port => SystemSetting.system_smtp_port,
:user_name => SystemSetting.system_smtp_username,
:password => SystemSetting.system_smtp_password,
:authentication => SystemSetting.system_smtp_authentication,
:enable_starttls_auto => SystemSetting.system_smtp_enable_starttls_auto }
end
But it appears that the database config has not been read at this point. I get an error:
ActiveRecord::ConnectionNotEstablished (ActiveRecord::ConnectionNotEstablished)
How can I accomplish this? Either make Rails initialize the database config first, or ... something else?

Regarding practices: Doing stuff like this can be problematic because your application relies on your database to be full when in fact it should support an empty database. Two examples that I can think of are when you try to create a database from scratch (e.g. rake db:setup), or in tests environments.
However, if have no other choice: I would move the mailer initialization to your system_setting.rb or an initializer (where you are guaranteed to have a connection).
config/initializers/load_system_settings.rb
MyApp::Application.config.action_mailer.raise_delivery_errors = true
MyApp::Application.config.action_mailer.delivery_method = :smtp
MyApp::Application.config.action_mailer.smtp_settings = {
:address => SystemSetting.system_smtp_host,
:port => SystemSetting.system_smtp_port,
:user_name => SystemSetting.system_smtp_username,
:password => SystemSetting.system_smtp_password,
:authentication => SystemSetting.system_smtp_authentication,
:enable_starttls_auto => SystemSetting.system_smtp_enable_starttls_auto
}

Manually connect and load values from database.
class Application < Rails::Application
# Set your app path
app_base = 'MY_APPLICATION_PATH'
# Load db configs
db_yml = YAML.load_file("#{app_base}/config/database.yml")["production"]
# Establish db connection
ActiveRecord::Base.establish_connection(:adapter=>db_yml['adapter'], :database=>db_yml['database'], :username=>db_yml['username'], :password=>db_yml['password'])
# load model if not
require "#{app_base}/app/model/system_setting.rb" unless defined?('SystemSetting')
#...normal config here...
config.action_mailer.raise_delivery_errors = true
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
:address => SystemSetting.system_smtp_host,
:port => SystemSetting.system_smtp_port,
:user_name => SystemSetting.system_smtp_username,
:password => SystemSetting.system_smtp_password,
:authentication => SystemSetting.system_smtp_authentication,
:enable_starttls_auto => SystemSetting.system_smtp_enable_starttls_auto }
#close sql connection after loaded
ActiveRecord::Base.connection.close
end

Related

Missing host to link to Rails heroku production

I have an app that I am trying to put onto production. When the user signs up they have an activation email that should be sent to their email accounts but I am getting stopped with this error:
ActionView::Template::Error(Missing host to link to! Please provide the :host parameter, set default_url_options[:host], or set :only_path to true)
The problem with this error is that I have everything set up correctly in my files. For example my production.rb file inside my environments folder looks like this:
config.action_mailer.raise_delivery_errors = true
config.action_mailer.delivery_method = :smtp
config.action_mailer.default_url_options = { :host => 'secret-everglades-54128.heroku.com'}
ActionMailer::Base.smtp_settings = {
:address => 'smtp.sendgrid.net',
:port => '587',
:authentication => :plain,
:user_name => ENV['SENDGRID_USERNAME'],
:password => ENV['SENDGRID_PASSWORD'],
:domain => 'heroku.com',
:enable_starttls_auto => true
}
My enviorments.rb file looks like this:
# Load the Rails application.
require_relative 'application'
# Initialize the Rails application.
Rails.application.initialize!
config.action_mailer.default_url_options = { :host => 'secret-everglades-54128.heroku.com'}
config.action_mailer.default_url_options = { :host => 'localhost:3000' }
This does not make any sense to me whatsoever, if anyone could shed some light on this problem I will be very great full.
The problem was that I had errors still concuring when I ran the command
git push heroku master
The problem was that I had my host set in the actually environment.rb file which wasn't needed so I removed that and once I did that I was able to get everything to work.

Completed 500 Internal Server Error in Rails 4 Devise Signup process

When I go to signup a new user on the site I fill in the form, click submit, and it sits there for a few seconds, then dispays 'We're sorry, but something went wrong.'
The Log is below...
https://gist.github.com/th3cuda/225f1352a7cfeee3f8f8
This is deployed on digitalocean.
production.rb
# Use default logging formatter so that PID and timestamp are not suppressed.
config.log_formatter = ::Logger::Formatter.new
config.action_mailer.default_url_options = { :host => 'MY_HOST' }
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
:address => 'smtp.MY_DOMAIN.com',
:port => 465,
:domain => 'MY_DOMAIN',
:user_name => 'MY_EMAIL',
:password => 'PASSWORD',
:authentication => 'plain',
:enable_starttls_auto => true
}
end
Since it's taking a while, it could be looking for sendmail (it uses /usr/sbin/sendmail by default) to send the email.
I would make sure that it's in place, that it can be used by the app, and that the config/(Dev, Prod, etc.) allows it to deliver email.
Is any of that helpful? Let me know what you find so I can give a better answer.
Sorry to add another answer, but formatting here is important.
config.action_mailer.default_url_options = { :host => 'MY_HOST' }
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
:address => 'smtpout.secureserver.net',
:port => 80,
:domain => 'www.example.com',
:user_name => 'johndoe#example.com',
:password => 'yourpassword',
:authentication => 'plain'
}
end
Notice I removed the starttls option, and set port to 80. Obviously, replace with your real values. If you're using constants, please avoid using those until this gets resolved.

Unrecognized authentication type when doing an Auth to Exchange from Rails

I'm getting this error when trying to authenticate with Exchange Server from Ruby on Rails:
504 5.7.4 Unrecognized authentication
type
config.action_mailer.raise_delivery_errors = true
config.action_mailer.perform_deliveries = true
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
:address => "x.x.x.x",
:port => 25,
:user_name => "xxdomain\xxuser",
:password => "xxxxxx",
:authentication => :login,
:enable_starttls_auto => true
}
I've tried all sorts of combinations of configuration settings, including changing the settings to use "plain" authentication, adding the domain, setting enable_starttls_auto to true, false, and removing it entirely, removing the port. Nothing has worked.
Any ideas?
The below is from link:
Rails and Exchange Email Auth
1) Add the ruby-ntlm gem to your Gemfile and run bundle install.
2) Add the NTLM SMTP library to your config/environment.rb file.
# Load the rails application
require File.expand_path('../application', __FILE__)
require 'ntlm/smtp'
# Initialize the rails application
RailsApp::Application.initialize!
3) Set up your environment file to authenticate with NTLM.
config.active_support.deprecation = :notify
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
:address => '<your-email-server>',
:domain => '<your-domain>',
:user_name => '<your-username>',
:password => '<your-unencrypted-password>',
:port => 25,
:authentication => :ntlm
****replace values with you own***

How to setup Google Apps with ActionMailer with Rails 2.3.5?

I've got Google Apps setup with email for my domain, and now I need to configure ActionMailer to use it. But the info I've found seems to be conflicting. Can anyone tell me how exactly to set it up with Rails 2.3.5?
I faced the same problems and got it working with this:
Step 1. Add the following to your development environment:
ActionMailer::Base.delivery_method = :smtp
ActionMailer::Base.perform_deliveries = true
ActionMailer::Base.raise_delivery_errors = true
ActionMailer::Base.default_charset = "utf-8"
ActionMailer::Base.smtp_settings = {
:enable_starttls_auto => true,
:address => "smtp.gmail.com",
:port => "587",
:domain => "domain.com",
:authentication => :plain,
:user_name => "YOUR_USER_NAME", #should be you#domain.com
:password => "YOUR_PASSWORD_HERE"
}
The critical line is :enable_starttls_auto. You have to restart webrick after making changes here.
Step 2. Create a Model like project_mailer.rb
class ProjectMailer < ActionMailer::Base
def confirmation(project)
subject 'Your email subject'
recipients project.email
from 'you#domain.com'
body :project => "hi"
end
end
Step 3. Create a View like /views/project_mailer/confirmation.html.haml (or .erb). This is just a standard view file.
Step 4. Add a line in your controller like:
ProjectMailer.deliver_confirmation(#project)

Why wouldn't a specific environment override environment.rb in rails here?

I'm trying to work on a Rails site here in it's development environment, where I want to test email delivery, and I can't work out why properties declared in the main environment.rb aren't being overwritten by the more specific development.rb file that I presume would be loaded when booting a rails app.
My understanding here is that values in more specific environment config files like this should override the shared 'environment.rb' config file, so if I have declared some email settings like so in config/environment.rb...
ActionMailer::Base.smtp_settings = {
:address => 'smtp.hostingcompany.com',
:port => 25,
:domain => 'productiondomain.net',
:authentication => :login,
:user_name => "productiondomainmailer",
:password => "TOP_SEKRIT"
}
... then code here in config/environments/development.rb below should override the ActionMailer:Base.smtp_settings hash:
ActionMailer::Base.smtp_settings = {
:domain => 'developmentdomain.net'
}
However, when load the app in the development environment, or from script/console to check the value of ActionMailer::Base.smtp_settings[:domain], it's still listed as 'productiondomain.net'.
Why might this happening?
To set the configuration for your development environment put:
config.action_mailer.smtp_settings =
rather than:
ActionMailer::Base.smtp_settings =
in config/environments/development.rb
Note that in config/environments/development.rb you will need to specify the full set of settings and not just :domain as the hash overwrites the existing value rather than being merged with it.
And similarly, set the default in config/environments.rb by putting config.action_mailer.smtp_settings = inside the Rails::Initializer.run do |config| block.
This should work:
config.action_mailer.smtp_settings = {
:address => 'smtp.some_host.com',
:port => 25,
:domain => 'developmentdomain.net',
:authentication => :login,
:user_name => 'blabla',
:password => "some_pass"
}

Resources