【问题标题】:url helpers in assets that respect an application sub uri尊重应用程序子 uri 的资产中的 url 助手
【发布时间】:2012-10-04 15:52:50
【问题描述】:

我在 rails 3.2 应用程序中有一个 js.erb 文件,该文件使用 url 帮助程序在 jquery $.ajax 调用中填写 url 字段。当我在本地机器上使用正确指向 localhost:3000/path_to_method 的 path_to_method_url/path 时,它工作正常。当我将它放在具有子 uri 的暂存环境中时,它指向 example-staging.com/path_to_method 而不是 example-staging.com/sub-uri/path_to_method。有人知道如何让资产中的 url 助手尊重子 uri 吗?

【问题讨论】:

    标签: ruby-on-rails


    【解决方案1】:

    尝试覆盖您的 application_controller.rb 中的 default_url_options

    def default_url_options(options = {})
      {:suburi => get_sub_uri}
    end
    

    http://api.rubyonrails.org/classes/ActionView/Helpers/UrlHelper.html#method-i-url_options

    http://rails.rubyonrails.org/classes/ActionController/Base.html#M000467

    【讨论】:

      【解决方案2】:

      我遇到了同样的问题并通过将我的 js.erb 文件中的 url 帮助程序从 <action>_path 更改为 <action>_url 然后将默认主机(包括子 uri)添加到每个环境配置来解决它,如下所示:

      config/environments/development.rb

      Rails.application.routes.default_url_options[:host] = 'localhost:3000'
      

      config/environments/staging.rb

      Rails.application.routes.default_url_options[:host] = 'example-staging.com/sub-uri'
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-11-29
        • 2011-10-07
        • 1970-01-01
        • 1970-01-01
        • 2017-09-15
        • 2018-04-20
        • 2021-11-30
        • 1970-01-01
        相关资源
        最近更新 更多