【问题标题】:Separate CSS Folders for Separate Rails Controllers用于独立 Rails 控制器的独立 CSS 文件夹
【发布时间】:2014-10-07 19:10:31
【问题描述】:

有没有办法为单独的 Rails 控制器提供单独的 css 文件夹?我有两个我想结合的主题。一个用于我的家庭控制器,当用户未登录时。另一个用于我的仪表板控制器,当用户登录时。这两个都有多个 css 和 javascript 文件。

我知道如何使用一个 css 文件来做到这一点。示例:home 控制器只读取 home.css。

但我真的很想要一个用于家庭控制器的文件夹,因为我想用多个文件保持井井有条。

任何帮助将不胜感激。

【问题讨论】:

    标签: css ruby-on-rails-4


    【解决方案1】:

    在我发布此消息后,我想到了一个好主意。这是给遇到这个问题的其他人的。

    删除全部要求。所以从 application.js 中删除这一行

    //= require_tree .
    

    并从 application.css 中删除这一行

    *= require_tree .
    

    在您的 application.html.erb 中更改为如下所示

      <%= stylesheet_link_tag "application", params[:controller], :media => "all" %>
      <%= javascript_include_tag "application", params[:controller] %>
    

    将此添加到您的 config/initializers/assets.rb

    %w( controller_one controller_two controller_three ).each do |controller|
      Rails.application.config.assets.precompile += ["#{controller}.js", "#{controller}.css"]
    end
    

    **用你的控制器名称替换控制器一、二等。

    现在是最酷的部分。将子文件夹添加到样式表作为您的控制器名称。在示例中,我的是“家”。将您的 css 文件添加到其中。获取您的 home.css(在您的主样式表文件夹中)并为其提供以下代码:

    /*
     * This is a manifest file that'll be compiled into application.css, which will include all the files
     * listed below.
     *
     * Any CSS and SCSS file within this directory, lib/assets/stylesheets, vendor/assets/stylesheets,
     * or vendor/assets/stylesheets of plugins, if any, can be referenced here using a relative path.
     *
     * You're free to add application-wide styles to this file and they'll appear at the bottom of the
     * compiled file so the styles you add here take precedence over styles defined in any styles
     * defined in the other CSS/SCSS files in this directory. It is generally better to create a new
     * file per style scope.
     *
     *= require home/freelancer
     *= require home/bootstrap
     *= require home/font-awesome
     */
    

    将“freelancer”替换为您的文件名。我在样式表/主文件夹中的文件名是 freelancer.css

    我相信有一种方法可以要求子文件夹中的所有内容,但我并不肯定。喜欢

    *= require home/.
    

    但不是正面的。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-02-04
      • 2021-02-15
      • 2010-12-17
      • 1970-01-01
      • 1970-01-01
      • 2011-09-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多