新手问题 Rails 4 下 JS 与 CSS 引用问题

xautjzd · 2014年04月30日 · 最后由 xautjzd 回复于 2014年05月01日 · 6347 次阅读

用的环境是 Rails4, 已经将要引用的 js 与 css 分别放置到 app/assets/javascripts/ 与 app/assets/stylesheets/目录下,并且已经在 application.js 与 application.css 中 require 过,用stylesheet_link_tagjavascript_include_tag在 View 文件中引用。运行程序后,出现:

Asset filtered out and will not be served: add `Rails.application.config.assets.precompile += %w( bootstrap.css )` to `config/initializers/assets.rb` and restart your server

网上找了好久,没找到解决办法,如果按照上面的提示添加的话,出现的效果与预期引用的 js 和 css 效果不符,同时 config/initialzers 目录下也没有 assets.rb 文件,冒然添加不知道会不会不妥。请大家帮忙指点下

Rails.application.config.assets.precompile += %w( bootstrap.css ) bootstrap.css 要写明路径

那这样如果 css 和 js 较多时,那么每一个都要写,这样也不太现实

config.assets.precompile << Proc.new do |path|
  if path =~ /\.(css|js)\z/
    full_path = Rails.application.assets.resolve(path).to_path
    app_assets_path = Rails.root.join('app', 'assets').to_path
    if full_path.starts_with? app_assets_path
      puts "including asset: " + full_path
      true
    else
      puts "excluding asset: " + full_path
      false
    end
  else
    false
  end
end 

#3 楼 @fresh_fish 在 config/initializers 目录下新建一个 assets.rb 文件,然后添加您这些代码么

需要 登录 后方可回复, 如果你还没有账号请 注册新账号