Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ArgumentError (Missing host to link to! Please provide the :host parameter, set default_url_options[:host], or set :only_path to true) #49

Closed
RSeven opened this issue Sep 29, 2020 · 9 comments

Comments

@RSeven
Copy link

RSeven commented Sep 29, 2020

Trying to use url helpers to get the full url inside a Notification returns an error because, apparently, default_url_options is not defined inside a Notification object.

def url
   video_show_url(slug: params[:video].slug)
 end

image

I fixed this by adding the following code inside my NewVideoNotification class.

private
  def default_url_options
    Rails.application.config.default_url_options
  end

Maybe this could be added to Noticed::Base.

@cabourn
Copy link

cabourn commented Oct 5, 2020

I may be wrong, but I've also been trying to "fix" this today. Personally I set the host option in the relevant environment files.

config/environments/development.rb

Rails.application.routes.default_url_options[:host] = 'localhost:3000'

@mybuddyandrew
Copy link

mybuddyandrew commented Oct 5, 2020

I had to set the default host to fix this as well.

@cabourn
Copy link

cabourn commented Oct 5, 2020

If that's the best solution, maybe just the docs can be updated under 'URL Helpers' to include the configuration?

@RSeven
Copy link
Author

RSeven commented Oct 5, 2020

I already have that setting, but it is slightly different, maybe that's it. This setting works for everything else, though, it just seems invisible inside a Notification class.

config/environments/development.rb

Rails.application.configure do
  config.default_url_options = { :host => 'localhost:3000', :protocol => 'http://', :locale => nil }
end

I may be wrong, but I've also been trying to "fix" this today. Personally I set the host option in the relevant environment files.

config/environments/development.rb

Rails.application.routes.default_url_options[:host] = 'localhost:3000'

@RSeven
Copy link
Author

RSeven commented Oct 5, 2020

Yeap, that solves it!

config/environments/development.rb

Rails.application.configure do
  routes.default_url_options[:host] = 'localhost:3000'
end

I still didn't really get the difference between this and my previous setting.

@RSeven RSeven closed this as completed Oct 5, 2020
@RSeven RSeven reopened this Oct 5, 2020
@excid3
Copy link
Owner

excid3 commented Oct 6, 2020

If you're going to try and access URLs in the console or in mailers, Rails requires you to set the default url options since it doesn't have a request to use the url from. Just standard Rails things, nothing specific to Noticed.

Definitely a point of confusion since it's not very clear in Rails: rails/rails#29992

We can add a note to the readme though so people don't forget.

@nikolaz90
Copy link

nikolaz90 commented Dec 10, 2022

Thanks for this!
solved problem!
Rails.application.configure do
routes.default_url_options[:host] = 'localhost:3000'
end

@Dujota
Copy link

Dujota commented Sep 23, 2024

small side note, if you do the config changes, make sure to restart the server. Dealt with this issue and accepted solution seemed to not work, but restarting server after config did the trick. In case anyone ran into problem but didnt fix.

@talhajunaid63
Copy link

talhajunaid63 commented Sep 24, 2024

I added the routes.default_url_options config in my config file, It is working fine locally but on staging/production its still throwing 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):

my config file looks like this:

  Rails.application.routes.default_url_options[:host] = ENV['HOST_DOMAIN']
config.action_mailer.default_url_options = {
    host: ENV['HOST_DOMAIN'], protocol: 'https'
  }

anyone else still facing this issue?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants