首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >单独的CSS文件夹用于单独的Rails控制器

单独的CSS文件夹用于单独的Rails控制器
EN

Stack Overflow用户
提问于 2014-10-08 03:10:31
回答 1查看 210关注 0票数 1

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

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

但我真的想要一个文件夹为家庭控制器,因为我想保持有组织的多个文件。

任何帮助都将不胜感激。

EN

回答 1

Stack Overflow用户

发布于 2014-10-08 06:34:04

在我发完这篇文章后,我想出了一个好主意。这是为其他有这个问题的人准备的。

删除全部必填项。因此,从application.js中删除此行

代码语言:javascript
运行
复制
//= require_tree .

并从application.css中删除此行

代码语言:javascript
运行
复制
*= require_tree .

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

代码语言:javascript
运行
复制
  <%= stylesheet_link_tag "application", params[:controller], :media => "all" %>
  <%= javascript_include_tag "application", params[:controller] %>

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

代码语言:javascript
运行
复制
%w( controller_one controller_two controller_three ).each do |controller|
  Rails.application.config.assets.precompile += ["#{controller}.js", "#{controller}.css"]
end

**替换controller_one、two等..使用您的控制器名称。

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

代码语言:javascript
运行
复制
/*
 * 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

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

代码语言:javascript
运行
复制
*= require home/.

但不是正面的。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26243434

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档