DeX3
DeX3

Reputation: 5539

Rails overriding default_url_options causes URL's like "localhost:2999:3000/..."

in my setup I've got nginx running on port 3000 acting as a proxy to my rails app running on port 2999. Obviously, I have to have the rails app use 3000 as the port for generated URL's (so that when I use login_url, it will generate an url like http://localhost:3000/login.

I overrid rails' default_url_options like this:

def default_url_options( options = {} )
  options.merge( { :only_path => false, :port => 3000 } )
end

However, this causes URL's like http://localhost:2999:3000/login for login_path.

Only thing I found was this ticket describing a related problem: https://rails.lighthouseapp.com/projects/8994/tickets/1106-rewrite_url-adds-port-twice, but there's nothing helpful in there.

Is there any way to make rails get the URL right?

thx in advance

Upvotes: 1

Views: 1121

Answers (1)

Seamus Abshere
Seamus Abshere

Reputation: 8516

If you know the hostnames for your different environments, you could set them statically, which routes around the problem:

# config/environments/production.rb
DEFAULT_HOST_WITH_PORT = 'http://myrealsite.com:3000'

# config/environments/development.rb
DEFAULT_HOST_WITH_PORT = 'http://localhost:3000'

# config/environments/test.rb
DEFAULT_HOST_WITH_PORT = 'http://localhost:3000'

and then

def default_url_options( options = {} )
  options.merge(:only_path => false, :host => DEFAULT_HOST_WITH_PORT)
end

(notice that you're not setting the port separately, you're just counting on it being included in host)

Upvotes: 2

Related Questions