42

In my app I have a few APIs that under api domain. Now in one of the API I want to generate a url that pointing to the main domain, say

test.com/blabla...

I tried to use url_for but seems the default root_url or request.host is in api domain. Url_for will make it to be

api.test.com/blabla..

while I want it to be

test.com/blabla...

Url_for can take a parameter

host: ...

to set it to be test.com/, the question is how can I get the root/base url (test.com) for host? root_url or request.host are all api.test.com.

Any ideas? Thanks.

Bruce Xinda Lin
  • 2,470
  • 6
  • 24
  • 37

4 Answers4

69

Just so that it's useful to someone else , i came across this today

request.base_url

gives the full path in local as well as on live .

request.domain

gives just the domain name so it sometimes kinda breaks the link while redirecting

Caffeine Coder
  • 1,729
  • 1
  • 15
  • 34
  • 5
    I get `NameError: undefined local variable or method 'request' for main:Object` for both of these. – Rockster160 Mar 31 '15 at 16:32
  • Make sure you are writing it in the controller and not in the view – Caffeine Coder May 05 '15 at 07:42
  • 1
    Use `self.request`. The request is not a local variable, it's an instance variable on a controller. – D3RPZ1LLA Jul 06 '15 at 18:09
  • 1
    You can use this in the view. It just has to be in the context of a request. I.e. it won't work in the Rails console or in a mailer or background job, etc. – stephen.hanson Apr 21 '16 at 20:23
  • 1
    Here's the documentation: http://www.rubydoc.info/github/rack/rack/Rack/Request/Helpers#base_url-instance_method – stephen.hanson Apr 21 '16 at 20:25
  • In my case I have only a single domain for any server instance, so I think it's safe to assign a global (eg $base_url = request.base_url) in a before_action method in application_controller.rb so I can access it anywhere downstream of any request. If it's possible that url could change in a single instance, that might be a bad idea. – jpa57 Jun 23 '18 at 15:25
  • @Rockster160 I had the same problem. The reason for that is the `request` object doesn't exist in the rails console (only in the controller). I found that out from [this](https://stackoverflow.com/questions/64322757/detect-base-url-in-controller/64323361#comment113743372_64323361) comment – stevec Oct 12 '20 at 18:37
28

According to this you can do request.domain

Tom Prats
  • 6,127
  • 6
  • 42
  • 73
14

Simplest alternative method:

include in you're class

include Rails.application.routes.url_helpers

create function or just use root_url to get app root/base url:

  def add_host_prefix(url)
    URI.join(root_url, url).to_s
  end

finally: add

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

in:

Your_project_root_deir/config/environments/development.rb

although helpers can be accessible only in views but this is working solution.

Kaleem Ullah
  • 5,548
  • 2
  • 37
  • 43
  • Most of this answer doesn't make sense, but I did find the `URI.join('www.newdomain.com', route_path)` concept helpful. – Ryanmt Feb 29 '16 at 17:43
  • 2
    This has some usefulness for those situations where you don't have a request object to work with. The big downside here is that `include Rails.application.routes.url_helpers` is beastly expensive. – jaydel Jan 09 '17 at 21:05
0

request.domain fails on CF it given domain url not base url

Mitesh Sharma
  • 95
  • 1
  • 8