rspec route testing and hosts - ruby-on-rails

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.

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

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'

Devise confirmation link is missing domain

Devise confirmation_url is producing just a relative url with no domain such as:
http://users/confirmation?confirmation_token=...
I tried changing
confirmation_url(#resources, :confirmation_token => #resource.confirmation_token)
to
confirmation_url(:confirmation_token => #resource.confirmation_token)
but it produces the same url. I upgraded to devise 2.2.3 but same outcome. Rails 3.1.4
Update:
I have set in my production.rb:
config.action_mailer.default_url_options = { :host => 'mysubdomain.mysite.com' }
and I tried setting
before_filter set_actionmailer_host
def set_actionmailer_host
ActionMailer::Base.default_url_options[:host] = request.host_with_port
end
to no avail in application controller (https://github.com/plataformatec/devise/issues/1567)
Update:
This occurs in both development and production.
Update
I can't understand why Devise isn't using the template in app/views/devise/mailer/confirmation_instructions.html.haml If I could edit that I could append the host manually: 'http://mysite.com/' + confirmation_url(...
I tried setting the scoped views setting but it didn't have any effect
This is a disaster, users can't confirm their registration :(
You might have missed to add the default url hosts to your environment config-files. When you run the rails g devise:install command you usually receive those instructions.
# config/environments/development.rb
# Default actiomailer url host (required by devise)
config.action_mailer.default_url_options = { host: 'myapp.dev' }
You need this setting for all your environments.
# config/environments/production.rb
# Default actiomailer url host (required by devise)
config.action_mailer.default_url_options = { host: 'myproductionapp.com' }

Rails / Devise / Mailer: Throwing 'config.action_mailer.default_url_options' error on attempted registration

It's just been one problem after another today. I just deployed to my production server and testing it out, with issues whenever anything involves email. Particularly with Devise's confirmable registration email, whenever I sign up for an account, the following error is thrown up in the log...
ActionView::Template::Error (You can no longer call ActionMailer::Base.default_url_options directly. You need to set config.action_mailer.default_url_options. If you are using ActionMailer standalone, you need to include the routing url_helpers directly.):
Looking at my config/environments/production.rb, I have the following set...
config.action_mailer.default_url_options = { :host => 'localhost' }
So config.action_mailer.default_url_options IS being used, but is being completely ignored. I tried changing the host to '127.0.0.1' and my server's IP addy, but it wouldn't have it. Threw it in config/application.rb, but no go. I hunted in my project for any other lines declaring default_url_options, but it's only set in the production.rb file. I Googled and found a post suggesting to set config.cache_classes to false, but negative there as well.
Now here is what's crazy...I got the same error whenever I tried to submit a brand new comment in my project (set up to send an email to confirm the email address if you are a first time commentor). I took OUT config.action_mailer...etc from the production.rb file, and viola - my comment mailer worked and sent the email just fine!
Still, Devise is being absolutely stubborn and keeps throwing the above error at me. Any ideas on why? BTW, all my gems are up to date. Below is my production.rb file.
--- UPDATE ---------------
Have made a little progress, but this is just weird. When I start the server and attempt to do something that will send mail (user registration, comment), I get the ActionMailer::Base error message above. When I try the action AGAIN...it works and the mail gets sent. From that point on (until Passenger and/or the server is rebooted), all mailer actions work fine.
But not without yet another issue (it never ends)...for some reason the email body is completely blank.
I ended up hitting this problem once again when I attempted to test in the test database, and I was determined once and for all to figure out why I was getting this error, and why no one else seemed to have come across it (very little results from Google).
After completely tearing down my project and reconstructing it, I finally pinpointed the issues to the Sitemap-generator plugin I had installed. Once I removed all traces of that, the above error finally ceased.
I'm working on this issue for Fat Free CRM, and I realized that this error happens because Rails 3 lazy-loads classes. All you need to do is 'touch' the ActionMailer::Base class in your environments/**.rb files, and it will load the class. Then you can call ActionMailer::Base.default_url_options from a controller, etc. (We do this so that we can automatically set the mailer host from request.host_with_port)
So my environment files now look like this:
FatFreeCRM::Application.configure do
...
ActionMailer::Base
end
I believe I have solved this issue...though I'm not "exactly" sure what did it. It very well may have been config.cache_classes = true which I commented out, and although I was concerned this would effect my site caching or something else, it doesn't seem to have broke it.
In addition, the empty email body I solved by switching from :sendmail to :smtp.
Here is my final production.rb file, hopefully this might be of use to someone with a similar issue in the future.
MyProject::Application.configure do
# Commented out, causes 'ActionView::Template' error
#config.cache_classes = true
config.whiny_nils = true
config.consider_all_requests_local = true
config.action_view.debug_rjs = true
config.action_controller.perform_caching = true
config.cache_store = :mem_cache_store
config.active_support.deprecation = :log
config.action_dispatch.best_standards_support = :builtin
config.action_mailer.raise_delivery_errors = true
Sunspot.config.solr.url = 'http://127.0.0.1:8080/solr'
Paperclip.options[:command_path] = "/usr/bin/"
config.action_mailer.perform_deliveries = true
config.action_mailer.delivery_method = :smtp
ActionMailer::Base.smtp_settings = {
:address => "smtp.gmail.com",
:enable_starttls_auto => true,
:port => 587,
:authentication => :plain,
:user_name => "user#domain.com",
:password => 'password'
}
config.action_mailer.default_url_options = { :host => 'dev.mydomain.com' }
config.time_zone = "Central Time (US & Canada)"
end

Resources