2010-09-20 73 views
5

是否有人知道在rails 3中將默認樣式表目錄/ public/stylesheets更改爲/ public/css?在rails中更改默認樣式表目錄

我發現了一個名爲 config.stylesheets_dir = '/css'

這並沒有工作,雖然可變。

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

回答

3

Javascript和樣式表的路徑沒有完全在Rails的dehardcoded 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 

和additionaly這一個,如果你使用它:

def stylesheet_path(source) 
    compute_public_path(source, 'stylesheets', 'css') 
    end 
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 

你要這樣,你可以以任何方式更改資產路徑和它將永遠向前兼容。

+2

我不知道爲什麼這是downvoted。這是一個更明確的解決方案,並且不太冒昧。 – Schrockwell 2012-09-30 21:45:20