【问题标题】:stylesheet_link_tag producing absolute links instead of relativestylesheet_link_tag 产生绝对链接而不是相对链接
【发布时间】:2011-02-21 17:07:38
【问题描述】:

我设置了 facebooker 来隧道我的 Ruby on Rails 应用程序。

问题是我想在本地进行测试。也就是说,我不想每次想看到我的更改时都必须启动隧道。

现在,当我使用 ruby script/server 启动应用程序(而不是事先调用 rake facebooker:tunnel:background_start)时,助手创建的链接(例如 stylesheet_link_tag、javascript_include_tag、image_tag)会在前面加上我的 tunnlr 地址:http://web1.tunnlr.com:myPort/。 (例如,页面源中的 CSS 链接如下所示:http://web1.tunnlr.com:myPort//stylesheets/appName.css?1234567890。)

我不想要那个功能;如果不先启动隧道,我看不到我的 CSS 或 JavaScript 更改。我希望链接是相对的,而不是绝对的。所以,stylesheet_link_tag 应该产生/stylesheets/appName.css?1234567890

有谁知道它为什么会这样做以及如何解决它?

提前致谢。

【问题讨论】:

    标签: ruby-on-rails facebooker tunneling


    【解决方案1】:

    AssetTagHelper 使用您的 asset_host URL。我不确定facebooker是否为你设置(我对facebooker了解不多),但你可以在你的视图中重置它没问题:

    在您的 stylesheet_link_tab 之前,只需覆盖主机 URL:

    ActionController::Base.asset_host = "localhost:3000"
    # or
    ActionController::Base.asset_host = ""
    

    所以使用 ERB 可能看起来像这样:

    <% ActionController::Base.asset_host = "" %>
    <%= stylesheet_link_tag "stylesheet.css" %>
    

    Walabing!

    【讨论】:

    • 最好把设置放到config/environments/development.rb中
    • +1 根据上下文,您可以使用ActionController::Base.asset_host = root_url[0..-2][0..-2] 修剪尾部斜杠)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-05-10
    • 2012-04-02
    • 1970-01-01
    • 2010-09-25
    • 2010-10-07
    • 2015-12-04
    • 2012-02-08
    相关资源
    最近更新 更多