rake task and action_mailer.default_url_options - ruby-on-rails

When I try to use ActionMailer from rake task I'm getting 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
In my development.rb I have the following line:
config.action_mailer.default_url_options = { :host => 'localhost:3000' }
Moreover, if I put in development.rb this:
config.action_mailer.default_url_options[:host] = 'localhost:3000'
rake task works fine, but server crashes with this:
config/environments/development.rb:20:in block in ': undefined method []=' for nil:NilClass (NoMethodError)
If I do this:
config.action_mailer.default_url_options = {}
config.action_mailer.default_url_options[:host] = 'localhost:3000
rake task doesn't work again.
I'm confused, any suggestions?
UPD
i don't think that code will help, but here it is, it's that simple:
rake task:
task :send_newsletter => :environment do
Receiver.all.each { |receiver|
NewsletterMailer.newsletter(receiver).deliver
}
end
mailer:
def newsletter(receiver)
#receiver = receiver
mail(to: receiver.email, subject: "newsletter") do |format|
format.html
end
end
and the view with some link_to xxx_url lines, apparently which causing the error
ruby version 2.4.1
rails 5.1.3
rake 12.0.0 (but also tried 12.3.0 with no difference)

Try the following, that should work
config.action_mailer.default_url_options = { host: 'localhost', port: 3000 }
Update
I don't know what is the ideal solution for this? but I have tested and it working for me
Rails.application.routes.default_url_options = config.action_mailer.default_url_options = { host: 'localhost', port: 3000 }
This also work
Rails.application.routes.default_url_options = Rails.application.config.action_mailer.default_url_options
This solution based on this link
And another one is
Rails.application.routes.default_url_options[:host] = '???'
This goes in each environment file - development.rb, test.rb and production.rb (and more if you have them) with the corresponding hostname in each one
Based Link
Hope it will help

solved with this line added to rake task. based on fool-dev's answer
Rails.application.routes.default_url_options = Rails.application.config.action_mailer.default_url_options

Related

Rails 5 default_url_options oddities

I have a pretty simple rails app that I'm working on upgrading from Rails 4 to Rails 5, but I'm noticing some weirdness with default_url_options
In config/environments/test.rb I have:
Rails.application.routes.default_url_options[:host]= ENV["HTTP_HOST"] || "localhost"
Rails.application.routes.default_url_options[:port]= ENV["PORT"] || 3000
My application has a namespace called api. In my request specs, I'm seeing this:
[1] pry> api_v3_sample_url
=> "http://www.example.com:3000/api/v3/sample"
[2] pry> Rails.application.routes.url_helpers.api_v3_sample_url
=> "http://localhost:3000/api/v3/sample"
What am I missing that is causing those URLs to be different?
EDIT
Per this thread I set
config.action_controller.default_url_options = {
host: ENV['HTTP_HOST'] || 'localhost'
}
in config/environments/test.rb but now I get this:
> Rails.application.routes.url_helpers.api_v3_sample_url
ArgumentError: Missing host to link to! Please provide the :host parameter, set default_url_options[:host], or set :only_path to true
> api_v3_sample_url
=> "http://www.example.com/api/v3/sample"
EDIT 2
Probably worth noting that these are request specs and not feature specs (not using capybara).
This should fix the issue in controller/request specs:
config.action_controller.default_url_options = {
host: ENV['HTTP_HOST'] || 'localhost'
}
As for why this is occurring, there's some more information available in this ongoing thread on Github.

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. :)

ActionView::Template::Error: Missing host to link to

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'

rspec route testing and hosts

I see I can test routes with rspec like this:
get("/").should route_to("welcome#index")
but I have constraints based on the hostname or parts of hostnames and redirects between several ones. How do I specify a hostname when testing?
How do I run the tests with proper configuration? I tried printing root_url and I got:
Missing host to link to! Please provide the :host parameter, set
default_url_options[:host], or set :only_path to true
The same error happens on mine whenever I run rspec spec/
The entire error is actually:
Failure/Error: #user = Factory(:user)
ActionView::Template::Error:
Missing host to link to! Please provide the :host parameter, set default_url_options[:host], or set :only_path to true
# ./app/views/devise/mailer/confirmation_instructions.html.erb:5:in `_app_views_devise_mailer_confirmation_instructions_html_erb__980323237__638702928'
# ./spec/models/campaign_spec.rb:21
The following line:
# ./app/views/devise/mailer/confirmation_instructions.html.erb:5:in `_app_views_devise_mailer_confirmation_instructions_html_erb__980323237__638702928'
actually gave me the hint that devise is the one throwing out the error.
Turns out I haven't set
config.action_mailer.default_url_options = { :host => 'localhost:3000' }
in config/environments/test.rb (only in development.rb)
Adding the config option cleared out the errors on mine.
I'm suspecting you're using other gems that requires the same option set.
None of the above worked for me. Adding the following to my spec_helper.rb (in my case spec/support/mailer.rb which is included in spec_helper.rb) fixed the error:
Rails.application.routes.default_url_options[:host] = 'test.host'
In my case I had to add
config.action_mailer.default_url_options = { :host => 'localhost:5000' }
to following to
config/environments/test.rb
because I was using FactoryGirl to generate a user without skipping the email_confirmation from user.

Resources