【问题标题】:Rails: How to override stylesheet_pathRails:如何覆盖 stylesheet_path
【发布时间】:2009-05-23 17:13:13
【问题描述】:

我一直在尝试覆盖 Rails 的 stylesheet_path 助手,但我还没有找到方法。我不能只打开 ActionView::Helpers::AssetTagHelper 模块并在那里覆盖它,因为 Rails 不会采用我的新方法。

我知道这可能是因为模块混入了,那么我该如何解决呢?

【问题讨论】:

    标签: ruby-on-rails ruby mixins


    【解决方案1】:

    您这样做是为了让stylesheet_link_tag 产生与正常情况不同的结果吗?如果是这样,只需在帮助程序中覆盖 that :)

    或者,如果您真的想覆盖stylesheet_path,您还需要重新定义别名,因为奇怪的是,它只能通过其别名访问(在 Rails 2.3.2 中)。例如,我把它放在environment.rb 中,它起作用了:

    module ActionView
      module Helpers
        module AssetTagHelper
          def stylesheet_path(source)
            "x"
          end
          alias_method :path_to_stylesheet, :stylesheet_path
        end
      end
    end
    

    我个人不会走这条路,但如果你需要它应该对你有用:)

    【讨论】:

      猜你喜欢
      • 2011-02-04
      • 1970-01-01
      • 2010-12-22
      • 1970-01-01
      • 2010-11-14
      • 1970-01-01
      • 1970-01-01
      • 2018-08-01
      相关资源
      最近更新 更多