单独的Rails控制器的CSS文件夹

时间:2014-10-07 19:10:31

标签: css ruby-on-rails-4

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

我知道如何使用一个css文件执行此操作。示例:home controller仅读取home.css。

但我真的很喜欢家庭控制器的文件夹,因为我希望保持多个文件的组织。

任何帮助都将不胜感激。

1 个答案:

答案 0 :(得分: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

**替换controller_one,两个,等等。使用您的控制器名称。

现在为酷的部分。将子文件夹添加到样式表作为控制器名称。我的家是&#34;家&#34;在示例中。将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
 */

替换&#34;自由职业者&#34;你的文件名是什么。我的stylesheets / home文件夹中的文件名是freelancer.css

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

*= require home/.

但不是积极的。