How do I set default host for url helpers in rails

2020-01-24 08:00发布

I would like to do something like this

config.default_host = 'www.subdomain.example.com'

in some of my configuration files, so that object_url helpers (ActionView::Helpers::UrlHelper) produce links beginning with http://www.subdomain.example.com

I have tried to search the docs but I did not find anything except ActionMailer docs and http://api.rubyonrails.org/classes/Rails/Configuration.html which is not useful for me, because I do not know in which pat to look. Is there a place which describes the whole structure of Rails::Initializer.config?

8条回答
不美不萌又怎样
2楼-- · 2020-01-24 08:13

Simplest way to get your dynamic domain url using a Global variable in rails.

class ApplicationController < ActionController::Base
  def base_url       
    $base_url = request.base_url
  end

end

After just call this method into your header file <% base_url %>, Now you can access your domain url anywhere in your application.

查看更多
太酷不给撩
3楼-- · 2020-01-24 08:14

NSD's solution is how I do it, but I had to add a block to make it work with https:

config.action_controller.asset_host = Proc.new { |source, request|
  (request ? request.protocol : 'http://') +  "www.subdomain.example.com"
}
查看更多
男人必须洒脱
4楼-- · 2020-01-24 08:18

You can easily set :host or/and :only_path parameter for every url_helper. yours_url(params, :host => "http://example.com", :only_path => Rails.env.test?) This way you are not setting global default_url_options in your environments, unless you want that.

查看更多
地球回转人心会变
5楼-- · 2020-01-24 08:21

asset_host doesn't work for urls

You need to override default_url_options in your ApplicationController (at least in Rails 3)

http://edgeguides.rubyonrails.org/action_controller_overview.html#default-url-options

class ApplicationController < ActionController::Base
  def default_url_options
    if Rails.env.production?
      {:host => "myproduction.com"}
    else  
      {}
    end
  end
end
查看更多
孤傲高冷的网名
6楼-- · 2020-01-24 08:21

Define the default host in your environment config:

# config/environments/staging.rb
MyApp::Application.configure do
  # ...
  Rails.application.routes.default_url_options[:host] = 'preview.mydomain.com'
  # ...
end

Then you can create a URL anywhere in your app:

Rails.application.routes.url_helpers.widgets_url()

Or include the URL helpers in your class:

class MyLib
  include Rails.application.routes.url_helpers

  def make_a_url
    widgets_url
  end
end

If you don't define the default host, you will need to pass it as an option:

widgets_url host: (Rails.env.staging? ? 'preview.mydomain.com' : 'www.mydomain.com')

It's also useful to specify things like the protocol:

widgets_url protocol: 'https'
查看更多
兄弟一词,经得起流年.
7楼-- · 2020-01-24 08:25

As far as I know, the *_url helpers use the server's configured host name. So for example if my Apache installation is accepting requests for this Rails app at http://www.myapp.com/ then Rails will use that address. That's why the *_url methods in a development environment point to http://localhost:3000 by default.

The asset host suggested in the previous answer will only affect the image_tag, stylesheet_link_tag and javascript_link_tag helpers.

查看更多
登录 后发表回答