rails get app root/base url

前端 未结 4 1485
隐瞒了意图╮
隐瞒了意图╮ 2020-12-29 18:53

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...
<         


        
相关标签:
4条回答
  • 2020-12-29 19:17

    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

    0 讨论(0)
  • 2020-12-29 19:25

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

    0 讨论(0)
  • 2020-12-29 19:28

    According to this you can do request.domain

    0 讨论(0)
  • 2020-12-29 19:37

    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.

    0 讨论(0)
提交回复
热议问题