2011-07-27 57 views
1

我在我的Ruby on Rails應用程序的輔助的unsecure_url獲取Ruby on Rails應用程序的URL?

def unsecure_url 
    "http://localhost:3000/" 
end 

然而,當應用程序是活的,這是錯誤的。我只是把應用程序在線,並想要做這樣的事情:

def unsecure_url 
    if is_production 
    production_url # <-- any way to determine this dynamically? 
    else 
    "http://localhost:3000/" 
    end 
end 

有關如何做到這一點的任何意見?

回答

0

我的理解是你只想得到當前頁面的URl?

def unsecure_url 
    if is_production 
    production_url = request.request_uri 
    else 
    "http://localhost:3000/" 
    end 
end 
+0

非常接近。我發現request.request_uri在RoR 3中被折舊 - 改爲使用request.fullpath。 – Geoff

1

如果你不需要完整的路徑,你可以使用root_url。這會給你http://localhost:3000/部分。如果您需要任何路徑信息(如/ blogs/123),則需要使用已提及的請求方法。