在rails中更改默认样式表目录

时间:2010-09-20 06:18:20

标签: ruby-on-rails css stylesheet

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

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

但这不起作用。

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

2 个答案:

答案 0 :(得分:3)

在Rails 3中,Javascript和样式表路径未完全解密。 要覆盖这些路径,您需要修补补丁(具有所有后果) 私人方法:

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

答案 1 :(得分:1)

或者,这就是我正在做的事情。我创建了一个可以像这样使用的包装器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

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