单独的 Rails 控制器的单独 CSS 文件夹 [英] Separate CSS Folders for Separate Rails Controllers

查看:54
本文介绍了单独的 Rails 控制器的单独 CSS 文件夹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Is there a way to have separate css folders for separate rails controllers? I have two themes I would like to combine. One for my home controller, when the user is not signed in. And one for my dashboard controller when the user is signed in. Both of these have multiple css and javascript files.

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

I know how to do this with one css file. Example: home controller only reads home.css.

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

But I would really like a folder for the home controller since I want to stay organized with multiple files.

任何帮助将不胜感激.

推荐答案

在我发布这篇文章后突然想到一个好主意.这适用于遇到此问题的其他人.

Just thought of a great idea after I posted this. This is for anyone else having this problem.

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

Remove the require all. So remove this line from application.js

//= require_tree .

并从 application.css 中删除这一行

And remove this line from application.css

*= require_tree .

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

Change to look like this in your application.html.erb

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

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

Add this to your 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、two 等.使用您的控制器名称.

**Replace controller_one, two, ect.. With you controller name.

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

Now for the cool part. Add a subfolder to stylesheets as your controller name. Mine was "home" in the example. Add your css files to it. Take your home.css (in your main stylesheets folder) and give it this code:

/*
 * 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".我在 stylesheets/home 文件夹中的文件名,对于这个文件,是 freelancer.css

Replace "freelancer" with what ever your file name is. My file name in my stylesheets/home folder, for this file, was freelancer.css

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

I believe there is a way to require all in subfolder, but I'm not positive. Like

*= require home/.

但不积极.

这篇关于单独的 Rails 控制器的单独 CSS 文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆