How to set config.action_controller.default_url_options = {:host = '#''} on per environment basis
Solution 1
Okay I figured it out the correct way to write it is
Rails.application.routes.default_url_options[:host] = 'localhost:3000'
:)
Solution 2
Inherit your Application's default_url_options
from ActionMailer
.
You want to keep things as DRY as possible so, ideally, you don't want to hard code your host and port in multiple places for the same environment, unless your ActionMailer
actually uses a different host and port than the rest of your Application
.
To set the default_url_options
for your entire Application
, simply add the following line to your config/environment.rb
file (changing MyApp
to your app's name):
# Set the default host and port to be the same as Action Mailer.
MyApp::Application.default_url_options = MyApp::Application.config.action_mailer.default_url_options
This will fix your problem and automatically set your Application
's default_url_options
to the same as your config.action_mailer.default_url_options
:
$ MyApp::Application.config.action_mailer.default_url_options
#=> {:host=>"lvh.me", :port=>"3000"}
$ MyApp::Application.default_url_options
#=> {:host=>"lvh.me", :port=>"3000"}
Solution 3
You have to restart your server before the changes to this file takes effect.
Solution 4
config.action_mailer.default_url_options = { :host => "your host" }
for instance your host localhost:3000
you can put this in test.rb, development.rb, production.rb files host could be different from environment to environment
Solution 5
config/environments/development.rb (any other environment, same)
add this row with host that you want
routes.default_url_options[:host] = 'localhost:3000'
trying_hal9000
Updated on July 05, 2022Comments
-
trying_hal9000 almost 2 years
Right now I'm using this which works for the development host, but I have to manually change the {:host => ""} code when I move to production.
post.rb
def share_all url = Rails.application.routes.url_helpers.post_url(self, :host => 'localhost:3000') if user.authentications.where(:provider => 'twitter').any? user.twitter_share(url) end end
I'd like to use this and then define the default_url_options per environment:
post.rb
def share_all url = Rails.application.routes.url_helpers.post_url(self) if user.authentications.where(:provider => 'twitter').any? user.twitter_share(url) end end
I've tried adding this to my config/environments/development.rb but I still get the "Missing host to link to! Please provide :host parameter or set default_url_options[:host]" error
development.rb
config.action_controller.default_url_options = {:host => "localhost:3000"}
And I even tried it this way:
development.rb
config.action_controller.default_url_options = {:host => "localhost", :port => "3000"}
EDIT:
I've now also followed this and still the same error guide http://edgeguides.rubyonrails.org/action_controller_overview.html#default_url_options
application controller
class ApplicationController < ActionController::Base protect_from_forgery include ApplicationHelper def default_url_options if Rails.env.production? { :host => "example.com"} else {:host => "example1.com"} end end end
This is driving me crazy, what am I missing here???
-
trying_hal9000 about 13 yearsI've totally restarted my server like 10 times trying this :) but no worky
-
Dmitry Polushkin over 12 yearsIf you have dynamic host's then it's not thread safe. I found only one way, is to set Thread.current in ApplicationController through before_filter, and use this host in Model.
-
sites almost 11 yearsis there a way to set it from config.routes or something like that?
-
lime over 10 years@juanpastas: I just added it to the top of
routes.rb
:Example::Application.routes.default_url_options[:host] = 'example.com'
-
engineerDave over 10 yearsFWIW I think is probably best addressed by using the _path helpers versus the url helpers... i.e. relative path versuse absolute url. :) This is usually a problem with rspec.
-
user3438601 over 9 yearsSwitching to _path helpers only helps so far. For instance, if you use redirect_to, it calls url_for under the hood so you need to pass :only_path => true each time you call it.
-
Jwan622 over 7 yearsWhere do you do this for rails engine?
-
Gabor Garami over 6 yearsAlso, using
_path
helpers is not working scenario if you have an ActiveJob what wants to use url helpers -
lulalala about 6 yearsIn 5.1.4 this does not work in web page rendering, but only work in rails console. See stackoverflow.com/a/48781000/474597 for more details.
-
new2cpp over 4 years
Rails.application.routes.default_url_options = { host: 'localhost:3000', protocol: 'https' }
if you want to change tohttps
too -
David Gay almost 4 yearsIf you do this, you might want to add a comment above your Action Mailer
default_url_options
stating that the Applicationdefault_url_options
inherit them. Wouldn't want someone changing the Action Mailerdefault_url_options
not realizing it will change them for the entire application. -
Joshua Pinter over 3 years@DavidGay Agree. Ideally, I would rather set the
default_url_options
explicitly and then specify thataction_mailer.default_url_options
inherit from those. For some reason, I didn't do it like that 2.5 years ago.