gpt4 book ai didi

Rails 中的 Css 命名空间

转载 作者:行者123 更新时间:2023-11-28 11:10:38 25 4
gpt4 key购买 nike

如何在 Rails 元素中将 namespace 添加到 css。

我有 2 个预定义的 css 文件和 2 个应用程序布局,我想第一个使用 1 个 css,另一个使用第二个。我的 css 文件具有相同类的样式,所以如果我不能以某种方式将它们分开,事情就会混淆。

我的一个 Controller 使用管理命名空间。我试图将第一个 css 复制到管理文件夹中,但它似乎不起作用。

文件夹结构:

stylesheets\admin\screen.css
\styles.css

views\layouts\application.html.haml
\aplication2.html.haml

---更新---

应用程序.html.haml:

%html
%head
%title Page
= stylesheet_link_tag "application", media: "all", "data-turbolinks-track" => true
= javascript_include_tag "application", "data-turbolinks-track" => true
= csrf_meta_tags
= favicon_link_tag 'favicon.ico'
%body
.wrapper
.page
.header-container
.header
.quick-access
%form#search_mini_form{action: "product_list.html", method: "get"}
.form-search
%label{for: "search"} Search:
%input#search.input-text{maxlength: "128", name: "q", type: "text", value: ""}/
%button.button{title: "Search", type: "submit"}
%span
%span Search
%p.welcome-msg Default welcome msg!
%ul.links
%li.first
%a{href: "dashboard.html", title: "My Account"} My Account
%li
%a.top-link-cart{href: "cart.html", title: "My Cart"} My Cart
%li.last
%a.top-link-checkout{href: "checkout.html", title: "Checkout"} Checkout
.nav-container
%ul#nav
- @categories.each do |cat|
%li.level0.nav-1.level-top.first
%a.level-top{href: "#"}
%span=cat.label
= yield
.col-left.sidebar
.col-right.sidebar
.block.block-cart
.block-title
%strong
%span My Cart
.block-content
%p.empty You have no items in your shopping cart.
.footer-container
.footer
%ul
%li
%a{href: "#"} About Us
%li
%a{href: "#"} Customer Service
%li.last.privacy
%a{href: "#"} Privacy Policy
%ul.links
%li.first
%a{href: "#", title: "Site Map"} Site Map
%li
%a{href: "#", title: "Search Terms"} Search Terms
%li
%a{href: "#", title: "Orders and Returns"} Orders and Returns
%li.last
%a{href: "#", title: "Contact Us"} Contact Us

如果我将 = stylesheet_link_tag "application", media: "all", "data-turbolinks-track"=> true 更改为 = stylesheet_link_tag "admin/screen.css", media : "all", "data-turbolinks-track"=> true 所有样式都丢失了。

最佳答案

在您的一个应用程序模板中放置 --

<%= stylesheet_link_tag    "admin/screen", :media => "all" %>

换句话说——

<%= stylesheet_link_tag    "styles", :media => "all" %>

如果您最终将 application.css 文件调用到两个模板中,您将看到它调用了整棵树 --

/*
*= require_self
*= require_tree .
*/

这会在每个现有样式表中调用布局。您只需要更具体地说明您在应用程序中调用了哪些样式表。

==============更新==============

由于您使用的是 Rails 4 和 HAML,因此您替换的行将如下所示 --

= stylesheet_link_tag "admin/screen", media: "all", "data-turbolinks-track" => true

= stylesheet_link_tag "styles", media: "all", "data-turbolinks-track" => true

当然,与适当的模板相对应。

============ 另一个更新 ===========

假设您有其他样式表想要包含在每个模板中,您可以通过用逗号分隔名称来包含它们。请参见下面的示例 --

= stylesheet_link_tag "styles", "another-stylesheet", "and-another-one" media: "all", "data-turbolinks-track" => true

关于Rails 中的 Css 命名空间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20950691/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com