Rails app/views/layouts/application.html.erb 报错

linusliu · 2014年07月09日 · 最后由 really530 回复于 2016年12月25日 · 5478 次阅读

自动生成的 application.html.erb 不停的报错,错误显示为:

Sprockets::Rails::Helper::AssetFilteredError in StaticPages#home
Showing C:/RailProject/sample_app/app/views/layouts/application.html.erb where line #7 raised:

Asset filtered out and will not be served: add `Rails.application.config.assets.precompile += %w( glyphicons-halflings-white.png )` to `config/initializers/assets.rb` and restart your server
  (in C:/RailProject/sample_app/app/assets/stylesheets/static_pages.css.scss)

这是第 7 行的内容: <%= stylesheet_link_tag "application", media: "all" %>

请教一下,这是怎么回事?

我找到一个帖子,说是把这两行内容换成如下两行就可以了,我试了一下,确实可以: <%= stylesheet_link_tag "application", media: "all", "data-turbolinks-track" => true %> <%= javascript_include_tag "application", "data-turbolinks-track" => true %> 换成 <%= stylesheet_link_tag "default", media: "all", "data-turbolinks-track" => true %> <%= javascript_include_tag "default", "data-turbolinks-track" => true %>

但我 css 和 js 文件夹中明明都是 application 啊?并没有 default 文件,请问这是怎么回事?

贴一下你的 config/environments/development.rb, static_pages.css.scss 的代码

config/environments/development.rb 内容如下:

Rails.application.configure do
  # Settings specified here will take precedence over those in config/application.rb.

  # In the development environment your application's code is reloaded on
  # every request. This slows down response time but is perfect for development
  # since you don't have to restart the web server when you make code changes.
  config.cache_classes = false

  # Do not eager load code on boot.
  config.eager_load = false

  # Show full error reports and disable caching.
  config.consider_all_requests_local       = true
  config.action_controller.perform_caching = false

  # Don't care if the mailer can't send.
  config.action_mailer.raise_delivery_errors = false

  # Print deprecation notices to the Rails logger.
  config.active_support.deprecation = :log

  # Raise an error on page load if there are pending migrations.
  config.active_record.migration_error = :page_load

  # Debug mode disables concatenation and preprocessing of assets.
  # This option may cause significant delays in view rendering with a large
  # number of complex assets.
  config.assets.debug = true

  # Adds additional error checking when serving assets at runtime.
  # Checks for improperly declared sprockets dependencies.
  # Raises helpful error messages.
  config.assets.raise_runtime_errors = true

  # Raises error for missing translations
  # config.action_view.raise_on_missing_translations = true
end

static_pages.css.scss 内容如下: // Place all the styles related to the StaticPages controller here. // They will automatically be included in application.css. // You can use Sass (SCSS) here: http://sass-lang.com/ @import "bootstrap";

请不要重复发帖,之前设置 NoPoint 是因为你描述不清楚

你用的是 Rails 4.1.4 . 详细看错误:

Asset filtered out and will not be served: add `Rails.application.config.assets.precompile += %w( glyphicons-halflings-white.png )` to `config/initializers/assets.rb` and restart your server
  (in C:/RailProject/sample_app/app/assets/stylesheets/static_pages.css.scss)

你添加了一个 static_pages.css.scss 并且直接在 erb 里面引用了,但你却没有将其加入 precompile 中。强制你必须加入。

解决方案:

app_name/config/initializers/assets.rb:

Rails.application.config.assets.precompile += %w(static_pages.css)

并且重新启动项目。

我刚刚解决。在 config\application.rb 中添加: config.assets.precompile += %w(*.png *.jpg *.jpeg *.gif)

参考 https://github.com/railstutorial/sample_app_rails_4/blob/master/config/application.rb

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