gpt4 book ai didi

css - Rails 3.2.2 无法使用 css

转载 作者:太空宇宙 更新时间:2023-11-04 13:13:40 26 4
gpt4 key购买 nike

我有一个问题。我在 Ruby on Rails 3.2.2 中做一个程序,但是页面 index.html.erb没有指向位于 app/assets/stylesheets/welcome.css.scss 中的样式表的链接

这是我的页面索引 localhost:3000/welcome/index

<h1>Bienvenido al curso de Ruby on Rails 3</h1>
<h2>Tutor: <%= @tutor %></h2>
<p>Esta es mi primera vista</p>
<footer>
<p>By : <%= @tutor %></p>
</footer>

源代码

    <!DOCTYPE html>
<html>
<head>
<title>HelloWorld</title

<link href="/assets/application.css?body=1" media="all" rel="stylesheet" type="text/css" />

<script src="/assets/application.js?body=1" type="text/javascript"></script>
<meta content="authenticity_token" name="csrf-param" />
<meta content="Jhnt5tNWpHa2lbZhPDSSwPJqdtPp94m9/ZrzZaA9054=" name="csrf-token" />
</head>
<body>

<h1>Bienvenido al curso de Ruby on Rails 3 </h1>
<h2>Tutor: Código Facilito</h2>
<p>Esta es mi primera vista</p>

<footer>
<p>By : Código Facilito</p>
</footer>
</body>
</html>


<link href="/assets/application.css?body=1" media="all" rel="stylesheet" type="text/css" />

源码中加载了app/assets/stylesheets/welcome.css.scss这个页面仅加载 app/assets/stylesheets/aplication.css

application_controller.rb

Layout
app/views/layouts/application.html.erb


<!DOCTYPE html>
<html>
<head>
<title>HelloWorld</title

<%= stylesheet_link_tag "application", :media => "all" %>
<link rel="stylesheet" type="text/css" href="/assets/welcome.css.scss?body=1">

<%= javascript_include_tag "application" %>
<%= csrf_meta_tags %>
</head>
<body>

<%= yield %>

</body>
</html>

File application.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 top of the
* compiled file, but it's generally better to create a new file per style scope.
*
*= require_self
*= require welcome
*= require_tree .
*/

最佳答案

出于多种原因,这不是您在 Rails 中包含 CSS 文件的方式。

首先,你的路径是错误的。您不能直接链接到 /assets/*,因为那不是将在生产中使用的 URL。 Assets 将被编译、缩小和指纹识别。您需要使用 asset_path,否则当您在生产环境中部署应用程序时,您的 Assets 路径将会中断。

其次,提供给浏览器的文件不再具有 scss 扩展名。这将是一个 .css 文件。

第三,你不应该手动添加 ?body=1 到你的样式表。如果您正确包含样式表,Rails 会在适当的时候为您做这件事。

有两种方法:

首先,您可能想要的是在 application.css 中要求额外的 CSS 文件。这就是它的用途,它是一个 list ,用于指定要包含的其他 CSS 文件。为此,请将以下内容添加到 application.css,或修改该文件中现有的 require 语句以包含额外的 require welcome 行:

/**
*= require welcome
*/

第二种方法是直接包含文件,但使用 Rails 助手来获取正确的路径和文件名:

<%= stylesheet_include_tag 'welcome' %>

两种方式都行。

关于css - Rails 3.2.2 无法使用 css,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24522058/

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