ActionView::Template::Error: Missing host to link to - ruby-on-rails

As the title implies, I'm getting this error when I try to use link_to in my mailer templates:
ActionView::Template::Error: Missing host to link to! Please provide the :host parameter, set default_url_options[:host], or set :only_path to true
I tried to follow it's instructions, and I also found this post, but I'm still getting the error.
I've tried adding config.action_mailer.default_url_options = { host: 'example.com' } to each of the following files and none of them worked:
/config/environment.rb
/config/application.rb (inside the module for my app)
/config/development.rb (I'm in development mode)
Has something changed in Rails 4 that makes this work differently?

I ran into this problem the other day and this is what ended up working for me:
Rails.application.routes.default_url_options[:host] = '???'
edit
This goes in each environment file - development.rb, test.rb and production.rb (and more if you have them) with the corresponding host name in each one

You've to set the default_url in each environment(development, test, production).
You need make these changes.
config/environments/development.rb
config.action_mailer.default_url_options =
{ :host => 'your-host-name' } #if it is local then 'localhost:3000'
config/environments/test.rb
config.action_mailer.default_url_options =
{ :host => 'your-host-name' } #if it is local then 'localhost:3000'
config/environments/production.rb
config.action_mailer.default_url_options =
{ :host => 'your-host-name' } #if it is local then 'localhost:3000'

Related

Missing host to link to! Please provide the :host parameter, for Rails 4

Missing host to link to! Please provide the :host parameter, set
default_url_options[:host], or set :only_path to true
I randomly get this error at time, generally restarting the server fixes the issue for a while, and then it shows up again.
I have added
config.action_mailer.default_url_options = "localhost:3000", in the development and test.rb files.
Also, I have used include Rails.application.routes.url_helpers
in one module to get access to the routes, I read this could be the reason I get these errors but removing it will leave me with no access to the routes.
The module is for the datatables gem.
For Rails 5, use:
Rails.application.routes.default_url_options[:host] = "XXX"
You can put this in a config/environments/ file (or any other initializer), or e.g. at the beginning of config/routes.rb.
You should write in the following way
For Development(development.rb)
config.action_mailer.default_url_options = { :host => "localhost:3000" }
In production (production.rb)
config.action_mailer.default_url_options = { :host => "myproductionsite.com" }
I had a similar error. The problem is in your configuration.
Try rewriting your configuration for your development.rb and test.rb like this:
config.action_mailer.default_url_options = { host: "localhost", port: 3000 }
Also check that your configuration in production.rb is written correctly like this:
config.action_mailer.default_url_options = { host: 'myherokuapp.herokuapp.com' }
You have updated the default url options for action mailer.
URL helpers will take the option from action_controller settings.
config.action_controller.default_url_options = .....
BR

Changed the domain name of heroku ruby on rails app, user mailer emails sending links to wrong domain? [duplicate]

I am trying to push my app on heroku. I am still in dev.
I use devise with the confirmable module.
When I try to add a user with the heroku console I got this error:
Missing host to link to! Please provide :host parameter or set default_url_options[:host]
in test and dev environment i have the following line:
environments/development.rb and environments/test.rb
config.action_mailer.default_url_options = { :host => 'localhost:3000' }
I don't have set up something in the production environment.
I've tried to push with
config.action_mailer.default_url_options = { :host => 'mywebsitename.com' }
config.action_mailer.default_url_options = { :host => 'heroku.mywebsitename.com' }
but it doesn't work too..
I see on the web that it could be related to ActionMailer but I don't know what I have to configure.
Many thanks for your idea!
EDITED:
Hi,
In order to not make my app crashes when I push on heroku I put this in my env/test.rb and my env/dev.rb (not in env.rb I think it is because it's a rails 3 app)
config.action_mailer.default_url_options = { :host => 'yourapp.heroku.com' }
But when I tried to create a user in the heroku console:
User.create(:username => "test", :email => "test#test.com", :password => "test1234", :password_confirmation => "test1234", :confirmed_at => "2010-11-03 14:11:15.520128")
here are errors I got:
ActionView::Template::Error: Missing host to link to! Please provide :host parameter or set default_url_options[:host]
/home/slugs/.../mnt/.bundle/gems/ruby/1.8/gems/actionpack-3.0.0/lib/action_dispatch/routing/route_set.rb:473:in `url_for'
/home/slugs/.../mnt/.bundle/gems/ruby/1.8/gems/actionpack-3.0.0/lib/action_dispatch/routing/url_for.rb:132:in `url_for'
/home/slugs/.../mnt/.bundle/gems/ruby/1.8/gems/actionpack-3.0.0/lib/action_view/helpers/url_helper.rb:99:in `url_for'
/home/slugs/.../mnt/.bundle/gems/ruby/1.8/gems/actionpack-3.0.0/lib/action_dispatch/routing/route_set.rb:195:in `user_confirmation_url'
EDITED (2)
When I type heroku logs on console I got this ==> production.log <== So I think when one deploys on heroku it's already in production.
I configure the env/prod.rb like this:
config.action_mailer.default_url_options = { :host => 'yourapp.heroku.com' }
and now I have this as error when I try to create a User:
Errno::EAFNOSUPPORT: Address family not supported by protocol - socket(2)
/usr/ruby1.8.7/lib/ruby/1.8/net/smtp.rb:551:in `initialize'
/usr/ruby1.8.7/lib/ruby/1.8/net/smtp.rb:551:in `open'
/usr/ruby1.8.7/lib/ruby/1.8/net/smtp.rb:551:in `do_start'
/usr/ruby1.8.7/lib/ruby/1.8/timeout.rb:62:in `timeout'
You need to add this to your environment.rb
config.action_mailer.default_url_options = { :host => 'localhost' }
Make sure you change host to your production url and keep it localhost for development. This is for the mailer, it needs a default email to send out notices such as confirmations etc...
You should check the logs on the heroku server heroku logs run that from the console and it will tell you the exact error.
When you push to heroku you need to configure the environment.rb file with the heroku subdomain:
config.action_mailer.default_url_options = { :host => 'yourapp.heroku.com' }
Depending upon version, this should go in production.rb, not environment.rb.
Ok,
First you have to install the sendgrid gem with this command line:
heroku addons:add sendgrid:free
Then you just have to configure your env/dev.rb and env/prod.rb like this:
env/dev.rb
config.action_mailer.default_url_options = { :host => 'localhost:3000' }
env/prod.rb
config.action_mailer.default_url_options = { :host => 'yourapp.heroku.com' }
Push on git and heroku. It should work..
Codeglot's anwser above does the job, but we wanted something a bit more flexible, so we did this:
On Heroku, we run multiple Production environments for staging and testing, so we need a flexible solution for the production.rb environment file.
In production.rb
config.action_mailer.default_url_options = { :host => ENV['MAILER_URL'] }
Then set the MAILER_URL environment variable for your app like so
heroku config:set MAILER_URL=my-awesome-app.herokuapp.com --app my-awesome-app
If you're running on Cedar:
run heroku addons:add sendgrid:free from your console.
Add the following lines to config/environments/production.rb in your app.
.
ActionMailer::Base.smtp_settings = {
:address => 'smtp.sendgrid.net',
:port => '587',
:authentication => :plain,
:user_name => ENV['SENDGRID_USERNAME'],
:password => ENV['SENDGRID_PASSWORD'],
:domain => 'heroku.com'
}
ActionMailer::Base.delivery_method = :smtp
config.action_mailer.default_url_options = { :host => 'YOUR-DOMAIN-HERE.COM' }
I had to do a number of things to get it to work in the production environment:
Inside of my production.rb file (/config/environments/production.rb) I added the following:
Rails.application.routes.default_url_options[:host] = 'myappsname.herokuapp.com'
config.action_mailer.delivery_method = :smtp
config.action_mailer.perform_deliveries = true
config.action_mailer.raise_delivery_errors = false
config.action_mailer.default :charset => "utf-8"
This is with Rails 4 and Devise 3
The working one after so many research,
Don't forget to add default from: mail address in your ApplicationMailer (application_mailer.rb) as,
class ApplicationMailer < ActionMailer::Base
default from: 'yourmail#gmail.com'
layout 'mailer'
end
Add the below configuration in your production.rb.
config.action_mailer.default_url_options =
{ :host => 'yourapp.herokuapp.com' }
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
address: 'smtp.gmail.com',
port: 587,
domain: 'heroku.com',
user_name: 'yourmail#gmail.com',
password: 'yourgmailpassword',
authentication: 'login',
enable_starttls_auto: true
}
Enable IMAP from your Gmail settings in Forwarding IMAP/POP tab.
Allow less secure apps: ON from https://myaccount.google.com/lesssecureapps
You're now good to go. :)

what is my heroku hostname? development is localhost:3000, what's production name?

Setting up a mailer. It works in localhost, however, it doesn't send the email, just gives me the message in the log. My development.rb is:
config.action_mailer.default_url_options = { :host => "localhost:3000" }
I tried this for my production.rb but it didn't work.
config.action_mailer.default_url_options = { :host => "http://cryptic-reef-1625.herokuapp.com/" }
I get an error when I hit the button that sends the email.
You need to add this to your environment.rb
config.action_mailer.default_url_options = { :host => 'localhost' }
Make sure you change host to your production url and keep it localhost for development. This is for the mailer, it needs a default email to send out notices such as confirmations etc...

Missing host to link to! Please provide the :host parameter,

I know this issue is there a thousand times. But mine seems strange :-)
I am using heroku. I wanna mail via SMTP. What are my correct settings for sending emails?
Please keep in mind, that my MailService is something like "gmail" or "gmx" and not my host superapp.herokuapp.com
my application.rb:
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
from: ENV['MAIL_SENDER_EMAIL_ADDRESS'],
address: ENV['MAIL_SENDER_SERVER_HOST'],
authentication: :login,
enable_starttls_auto: ENV['MAIL_SENDER_TLS'],
user_name: ENV['MAIL_SENDER_USERNAME'],
password: ENV['MAIL_SENDER_PASSWORD']}
config.action_mailer.default = {
:charset => "utf-8",
from: ENV['MAIL_SENDER_EMAIL']}
config.action_mailer.raise_delivery_errors = false
config.action_mailer.perform_deliveries = true
in my production.rb
config.action_mailer.default_url_options = {host: ENV['RAILS_HOST']}
config.action_mailer.raise_delivery_errors = false
config.action_mailer.perform_deliveries = true
What is the right config to get rid of that 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)
ENV['RAILS_HOST'] = "superapp.herokuapp.com" does not work
Loading it as an ENV variable caused it to throw the error for me and confused me for the past few hours.
In the end I added the following with the correct value to each environment file:
test.rb / development.rb / production.rb
Devise 3.2.x
Rails 4.1.x
Thanks to maudulus
# Default Mailer Host
Rails.application.routes.default_url_options[:host] = 'domain.com'

in rails set config.action_mailer.default_url_options to whatever the current host of the app is?

How can I dynamically set the host in a config/environments/production.rb:
config.action_mailer.default_url_options = { :host => THE_HOST}
This is so the app works correctly when in staging and when in production. Our staging server is stage.app.com, and links need to go there.
In config/environments/production.rb do this:
config.action_mailer.default_url_options = { :host => 'app.com' }
and in config/environments/staging.rb do this:
config.action_mailer.default_url_options = { :host => 'stage.app.com' }
As #BrettBender commented:
You do not need to dynamically set the host. For an app in production, production.rb will be evaluated. For an app running in staging environment, rails will load the staging file automatically (same with development or any custom environments you define)
Add this to a before filter in application_controller:
ActionMailer::Base.default_url_options = {:host => request.host_with_port}

Resources