【问题标题】:Change default stylesheet dir in rails更改 rails 中的默认样式表目录
【发布时间】:2010-09-20 06:18:20
【问题描述】:

有人知道在 rails 3 中将默认样式表目录 /public/stylesheets 更改为 /public/css 吗?

我找到了一个名为 config.stylesheets_dir = '/css'

但这没有用。

我知道我可以做到<%= stylesheet_link_tag '/css/mystyle.css' %>,但我很好奇是否有更好的方法。

【问题讨论】:

    标签: ruby-on-rails css stylesheet


    【解决方案1】:

    JavaScript 和样式表路径在 Rails 3 中没有完全解码。 要覆盖这些路径,您需要猴子补丁(以及所有后果) 私有方法:

    module ActionView::Helpers::AssetTagHelper
        private
          def compute_stylesheet_paths(*args)
              expand_stylesheet_sources(*args).collect { |source| compute_public_path(source, 'stylesheets', 'css', false) }
          end
    end
    

    如果您使用它,还可以添加这个:

      def stylesheet_path(source)
        compute_public_path(source, 'stylesheets', 'css')
      end
    

    【讨论】:

      【解决方案2】:

      另外,这就是我正在做的事情。我创建了一个包装器asset_tag,可以像这样使用:

      <%= asset_tag 'mystyle', :css %>
      <%= asset_tag 'mycode', :js %>
      

      然后我在application_helper中定义它:

      module ApplicationHelper
      
        # here is where you define your paths
        # in this case, paths will be '/css/mystyle.css' and '/js/mycode.js'
        def asset_path(asset, type)
          return "/css/#{asset}.css" if type == :css
          return "/js/#{asset}.js" if type == :js
        end
      
        def asset_tag(asset, type)
          return stylesheet_link_tag asset_path(asset, type) if type == :css
          return javascript_include_tag asset_path(asset, type) if type == :js
        end
      
      end
      

      通过这种方式,您可以以任何方式更改资产路径,并且始终向前兼容。

      【讨论】:

      • 我不知道为什么这被否决了。这是一个更明确的解决方案,并且不那么 hacky。
      猜你喜欢
      • 1970-01-01
      • 2020-05-21
      • 2023-03-30
      • 1970-01-01
      • 1970-01-01
      • 2014-05-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多