【问题标题】:How to change the head section of Index.html in a Ruby on Rails app?如何在 Ruby on Rails 应用程序中更改 Index.html 的 head 部分?
【发布时间】:2016-11-30 02:08:45
【问题描述】:

在我的 Ruby on Rails 应用程序的 head 部分,当我在浏览器中查看源代码时,我只能看到以下 CSS 和 JS 文件。

<link rel="stylesheet" media="all" href="/stylesheets/default.css" data-turbolinks-track="reload" />

<script src="/javascripts/default.js" data-turbolinks-track="reload">

我已将引导 CSS 和 JS 文件添加到 app/assets/stylesheetsapp/assets/javascripts 并更改了 application.cssapplication.js 文件。我仍然无法在源代码中看到这些文件。

【问题讨论】:

  • 可以粘贴 default.js 、 default.css 的代码和 application.html.erb 的 header 吗?
  • @DivyangHirpara /* * 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 any plugin's vendor/assets/stylesheets directory 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
  • @divyangHirpara * compiled file so the styles you add here take precedence over styles defined in any other CSS/SCSS * files in this directory. Styles in this file should be added after the last require_* statement. * It is generally better to create a new file per style scope. * *= require bootstrap *= require default *= require_self *= require_tree . */
  • @Vucko 我不知道它为什么使用 default.css 以及在哪里找到该文件?我没有使用 bootstrap-sass gem,因为有人说通过这种方式添加文件并在 application.css/js 中要求它们更容易

标签: html css ruby-on-rails ruby twitter-bootstrap


【解决方案1】:

&lt;link rel="stylesheet" type="text/css" href="stylesheets/bootstrap.min.css"&gt; 添加到application.html.erb 文件中。

感谢@Deepak 引导我找到解决问题的替代方案。

【讨论】:

    【解决方案2】:
    1. 您需要使用 require_tree . 或明确要求这些文件

      application.css

      /*
      *= require self
      *= require_tree .
      */
      

      application.css

      //= require self
      //= require_tree .
      
    2. 你可以使用content_for标签

      application.html.haml

      %head
        = yield :head   
      

      index.html.haml

      = content_for :head do
        = javascript_include_tag "bootstrap"
      

    【讨论】:

    • 我需要这些文件,其次我使用的是 application.html.erb 格式。我必须在该文件中使用 content_for 吗?
    • 是的,如果您想从任何其他文件中添加一些内容到 head 中,那么您可以使用它。你也可以在 .erb 中使用它
    • 只需将上面的haml转换为erb
    • 嗯.. 我在公共目录中添加了样式表文件夹并在其中添加了引导文件。这样它就起作用了。而且我只在寻找application.html.erb 文件。我认为应该是index.html。感谢那。 :)
    【解决方案3】:

    在您的 application.css 添加以下内容以包含您的文件

    *= require bootstrap
    *= require default
    *= require_tree .
    */
    

    【讨论】:

    • 已添加: *= 需要引导程序 *= 需要默认值 *= 需要_self *= 需要_树。 */
    猜你喜欢
    • 2011-05-21
    • 2011-05-06
    • 1970-01-01
    • 2012-12-12
    • 1970-01-01
    • 1970-01-01
    • 2011-03-12
    • 2020-02-09
    • 2012-09-11
    相关资源
    最近更新 更多