【问题标题】:Stylesheets in Rails App are incorrectly scopedRails 应用程序中的样式表范围不正确
【发布时间】:2013-10-12 13:55:20
【问题描述】:

我有我的主应用程序 css,我想排除我的后端样式表,因为当 application.css 执行 *=require_tree . 时,所有样式都会溢出。

我目前的文件夹设置如下:

assets/   
   stylesheets/
       main/ #application.css ##other css files for front end
       backend/ #backend.css ##other css files for back end

我的application.css如下:

/*
*= require_self
*= require foundation_and_overrides
*= require_tree ./main/
*/

后端.css

/*
 *= require_self
 *= require foundation_and_overrides
 *= require_tree ./backend/
 */

在我的 application.rb 中

config.assets.precompile += ['application.css', 'backend.css']

我的发展.rb

 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

我尝试移动样式表并使用不同的目录,但我要么根本不加载样式表,要么收到“找不到foundation_and_overrides”...

有没有简单干净的方法来做到这一点? 我只想排除几个样式表与 application.css 一起编译

【问题讨论】:

    标签: css ruby-on-rails ruby-on-rails-4 sprockets ruby-2.0


    【解决方案1】:

    sprockets 的全部意义在于您包含您需要的内容。您始终可以将另一个样式表作为一个整体添加到页面中。与用户一起编译是一种很好的做法,但如果管理员使用后端,添加另一个样式表是完全可以的。

    首先,application.css 已经预编译,所以你只需要:

    config.assets.precompile << 'backend.css'
    

    可选地包括一个额外的样式表,所以application.css 有基础等,backend.css 有额外的样式:

    <%= stylesheet_link_tag 'application', media: :all %> 
    <% if admin? %>
       <%= stylesheet_link_tag 'backend', media: :all %> 
    <% end %>
    

    如果由于某种原因application.css 样式与backend.css 样式发生冲突,您还可以为两者的样式表添加shared 目录。

    + stylesheets
      + shared
      + app
      + backend
      - application.css
      - backend.css
    

    还有require_tree ./shared

    【讨论】:

      猜你喜欢
      • 2015-05-11
      • 2018-02-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-04-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多