I need to use the root_url method from a method defined in a file in the lib folder. Is that possible?
I tried including this line in my class:
include Rails.application.routes.url_helpers
but this gives me the error
Missing host to link to! Please provide :host parameter or set default_url_options[:host]
Edit: I found out that it works if I first initialize the routes:
def initialize_routes
if Rails.env.development? || Rails.env.test?
Rails.application.routes.default_url_options[:host] = 'localhost:3000'
elsif Rails.env.production?
Rails.application.routes.default_url_options[:host] = 'example.com'
end
end
Is there a better way to accomplish this? Maybe setting the routes in a config file?
There should be a couple solutions for this.
1) when using root_url pass in the host param like:
That would need to be made to be environment specific though.
2) You should also be able to set the routes default_url_options like:
3) Within your environment config files you should set the default_url_options as stated in the error. For example:
In
config/environments/development.rb:In
config/environments/test.rb:In
config/environments/production.rb: