2010-04-17 52 views
51

我願做這樣的事情如何在rails中爲url助手設置默認主機?

config.default_host = 'www.subdomain.example.com' 
在我的一些配置文件

,使object_url傭工(ActionView::Helpers::UrlHelper)產生與http://www.subdomain.example.com

我試圖開始搜索文檔的鏈接,但我沒有除了ActionMailer docs和http://api.rubyonrails.org/classes/Rails/Configuration.html之外沒有找到任何東西,這對我沒有用處,因爲我不知道要在哪個模式下看。有沒有一個描述Rails :: Initializer.config整個結構的地方?

回答

50

asset_host不會爲網址

你需要重寫default_url_options在工作您ApplicationController(至少在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 
+18

或者你可以在你的production.rb中設置 #production.rb config.action_controller.default_url_options = {host:'myproduction.com'} – goma 2015-02-03 03:57:06

+0

你好,很抱歉重新打開它,但我有一個快速的問題:我們可以把多個地址在:主機值? – user1713964 2016-05-04 08:46:16

-9

有這個,但我並不十分肯定,如果他們是你指的是助手:

ActionController::Base.asset_host = "assets.example.com" 

http://api.rubyonrails.org/classes/ActionView/Helpers/AssetTagHelper.html

+0

我的意思是幫手像在http://api.rubyonrails.org/classes/ActionView/Helpers/UrlHelper.html 我已經添加到我的問題。 – gorn 2010-04-17 22:48:51

+1

'asset_host'處理資產管道的主機,而不處理應用程序的路由。 – rxgx 2012-05-22 01:19:31

1

據我所知,*_url助手使用服務器的配置主機名。例如,如果我的Apache安裝在http://www.myapp.com/上接受對這個Rails應用程序的請求,那麼Rails將使用該地址。這就是爲什麼默認情況下,開發環境中的*_url方法指向http://localhost:3000

上一個答案中建議的資產主機只會影響image_tag,stylesheet_link_tagjavascript_link_tag助手。

+0

這是一個好主意,但至少在我的情況下它不起作用。我有ServerName subdomain.example.com的虛擬服務器和* _url助手只使用example.com。 我也試着看看環境變量,我得到的是作爲subdomain.example.com的HTTP_HOST,所以我很確定它是正確的。 – gorn 2010-04-17 22:47:36

-3

NSD的解決方案是我如何做到這一點,但我不得不增加一個塊,使其以https工作:

config.action_controller.asset_host = Proc.new { |source, request| 
    (request ? request.protocol : 'http://') + "www.subdomain.example.com" 
} 
+0

資產主機用於資產管道,而不是應用程序的路由。 – rxgx 2012-05-22 01:18:37

+1

對不起,這是僅限於2.x – simianarmy 2012-05-22 04:26:36

+0

如果您不懂語言,請不要低估 – simianarmy 2012-05-29 01:26:08

37

定義您的環境中配置的默認主機:

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

然後你就可以在你的應用程序的任何地方創建一個URL:

Rails.application.routes.url_helpers.widgets_url() 

或包含在類的網址助手:

class MyLib 
    include Rails.application.routes.url_helpers 

    def make_a_url 
    widgets_url 
    end 
end 

如果您未定義默認主機,則需要將其作爲選項通過:

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

它也是有用的,如協議規定的事情:

widgets_url protocol: 'https' 
+7

目前它的'Rails.application.default_url_options',沒有.routes – 2014-05-29 10:21:56

+1

@MarcinAdamczyk你的意思是在Rails 4中? – Andrew 2014-12-02 23:35:07

+0

肯定它在Rails 4中,我不記得這個選項在Rails 3中 – 2014-12-03 11:30:25

23

另一種方法是將其設置這樣

# config/production.rb 
config.action_controller.default_url_options = { host: 'myproduction.com' } 
+0

這不起作用 – vedant1811 2016-08-08 09:43:11

+4

這實際上是唯一對我有用的東西。 – CHawk 2016-08-11 22:29:23

+1

我和@chaawk有相同的經歷。 – 2016-10-19 18:28:13

0

最簡單的方式使用全局變量,讓您的動態域名網址在軌道中。

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

end 

後只需要調用這個方法到你的頭文件<% base_url %>,現在你可以在任何地方訪問你的域名網址在您的應用程序。

+0

爲什麼當你從不使用它的時候把值賦給全局變量? – gorn 2016-03-07 16:44:24

+0

因爲你可以在你的Rails應用程序的任何地方獲得你的基礎url。 – prasanthrubyist 2016-03-08 09:46:43

+0

這是次要的事情,但我真的不明白。要麼你應該簡單地返回request.base_url,或者你應該建議使用全局變量而不是方法本身。只是beeing挑剔,對不起.. :) – gorn 2016-03-24 12:47:40

2

您可以輕鬆地爲每個url_helper設置:host或/和:only_path參數。 yours_url(params, :host => "http://example.com", :only_path => Rails.env.test?) 這樣你就不會在你的環境中設置全局的default_url_options,除非你想要。

相關問題