Kopty
Kopty

Reputation: 548

How do I use link_to in a relative manner in Rails?

I am using omniauth-facebook gem to do some Facebook login stuff. The call works in such a way that when /auth/facebook is called, it does the authentication process. All of that works fine when I manually enter the URL: http://localhost:3000/auth/facebook

However, when I took my code only, my domain is setup such that my apps have URLs such as:

http://railsapps.mydomain.com/app1  
http://railsapps.mydomain.com/app2  
http://railsapps.mydomain.com/app3

etc.

Now in my index page I have a link that is written in my .html.erb file as such:

<%= link_to "Sign in with Facebook", "/auth/facebook", id: "sign_in" %>

When I run this locally and click on the link it correctly goes to localhost:3000/auth/facebook and everything is good in the world. However, when I go online with this same code and click on this link it goes to: http://railsapps.mydomain.com/auth/facebook (which of course doesn't work, since it should be going to railsapps.mydomain.com/app1/auth/facebook.

Any help that could be provided in this regard would be greatly appreciated.

Thank you.

Upvotes: 1

Views: 1613

Answers (2)

Kenny Grant
Kenny Grant

Reputation: 9623

You should be able to use this:

<%= link_to "Sign in with Facebook", root_url + "auth/facebook", id: "sign_in" %>

or if that doesn't work try request.host:

<%= link_to "Sign in with Facebook", "http://#{request.host}/auth/facebook", id: "sign_in" %>

Upvotes: 2

apneadiving
apneadiving

Reputation: 115511

You should edit your default_url_options in ApplicationController:

def default_url_options
  if Rails.env.production?
    {:host => "railsapps.mydomain.com/app1"}
  else  
    {}
  end
end

Upvotes: 1

Related Questions