rails get app root/base url

71,986

Solution 1

According to this you can do request.domain

Solution 2

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

Solution 3

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.

Share:
71,986

Related videos on Youtube

Bruce Lin
Author by

Bruce Lin

Updated on July 09, 2022

Comments

  • Bruce Lin
    Bruce Lin almost 2 years

    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.

  • Rockster160
    Rockster160 about 9 years
    I get NameError: undefined local variable or method 'request' for main:Object for both of these.
  • Caffeine Coder
    Caffeine Coder about 9 years
    Make sure you are writing it in the controller and not in the view
  • D3RPZ1LLA
    D3RPZ1LLA almost 9 years
    Use self.request. The request is not a local variable, it's an instance variable on a controller.
  • Ryanmt
    Ryanmt over 8 years
    Most of this answer doesn't make sense, but I did find the URI.join('www.newdomain.com', route_path) concept helpful.
  • stephen.hanson
    stephen.hanson about 8 years
    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
    stephen.hanson about 8 years
  • jaydel
    jaydel over 7 years
    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.
  • jpa57
    jpa57 about 6 years
    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.
  • stevec
    stevec over 3 years
    @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 comment