【问题标题】:CSS applied to all pages but one - Ruby/SinatraCSS 应用于除一个以外的所有页面 - Ruby/Sinatra
【发布时间】:2019-04-23 11:25:29
【问题描述】:

我正在使用 Sinatra 使用 Ruby 创建一个简单的 Web 应用程序,并且在修改视图时,CSS 将应用于除一个之外的所有 .erb 文件。我不太确定问题是什么。我试过了:

  • 清除我的浏览器缓存
  • 在新浏览器(Chrome、Safari、Firefox 甚至 Edge)上运行应用程序
  • 在不同的设备上运行应用程序
  • 直接从 erb 文件链接样式表

这是我的 layout.erb 文件

<br/>
<h2>Upload a Video</h2>
<br/>
<br/>
<form action="/create_video" method="POST">
  <div class="col-6">
    <label for="videoTitle">Video Title</label>
    <input type="text" class="form-control" aria-describedby="emailHelp" placeholder="Enter video title" name="title">
  </div>
  <br/>
  <div class="col-6">
    <label for="videoDescription">Video Description</label>
    <input type="text" class="form-control" placeholder="Enter video description" name="description">
  </div>
  <br/>
    <br/>
    <div class="col-6">
     <label for="videoURL">Video Link</label>
     <input type="text" class="form-control" placeholder="Enter link to video" name="l_url">
    </div>
    <br/>
    <div class="col-6">
     <label for="listingUrl">Company</label>
     <input type="text" class="form-control" placeholder="Enter company name" name="company">
    </div>
    <br/>
  &nbsp;&nbsp;<button type="submit" class="btn btn-primary">Submit</button>
</form>
<br/>
<br/>
</div>

我的 layout.erb 文件

<!DOCTYPE html>
<html>
<head>
    <title></title>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
    <link href="https://fonts.googleapis.com/css?family=Roboto:300|Niramit|Nunito" rel="stylesheet">
    <link rel="stylesheet" type="text/css" href="style.css">
    <link rel="stylesheet" type="text/css" href="animate.css">
</head>
<body>

    <%= erb :navigation %>

    <main role="main">

        <%= yield %>

    </main>


    <footer class="end">
         <div class="footer-grid">
      <div class="footer-one">
        <p> logo<p>
      </div>
        <div class="footer-two">
            <p>&copy; Copyright Ignite 2018. All rights reserved.</p>
            <p>Terms of use • Press Inquiries • General Inquiries • Investors</p>
        </div>
        <div class="footer-three">
        <img src="https://image.flaticon.com/icons/svg/185/185981.svg" width=40 height=40>
        &nbsp;&nbsp;&nbsp;&nbsp;
        <img src="https://image.flaticon.com/icons/svg/185/185961.svg" width=40 height=40>
        &nbsp;&nbsp;&nbsp;&nbsp;
        <img src="https://image.flaticon.com/icons/svg/185/185985.svg" width=40 height=40>
        </div>
    </footer>


    <script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>
</body>
</html>

最后,这是一个样式表工作的小 sn-p:

   <br/>
   <br/>
   <h2>Videos</h2>
   <br/>
   <br/>
   <% @videos.each do |v| %>
      <img src="<%= v.logo %>" width="110" height="110">
      <h3><%= v.title %></h3>
      <p><%= v.l_url %></p>
      <p><%= v.description %> </p>
   <br/>
   <% end %>
   <br/>
   <br/>
   <br/>
   <br/>
</div>

知道可能出了什么问题吗?我很感激任何提示。

【问题讨论】:

  • 浏览器控制台有错误吗?
  • 没有应用 CSS 的页面的路径(URL)是什么?
  • 所以我刚刚检查了浏览器控制台,我收到了这个错误:拒绝应用来自 'localhost:4567/videos/style.css' 的样式,因为它的 MIME 类型 ('text/html') 不是受支持的样式表MIME 类型,并启用严格的 MIME 检查。你知道我该如何解决这个问题吗?
  • @timbillstrom 它适用于除“localhost:4567/upload_video”以外的所有路径
  • @slope 您能否发送一个 CSS 实际上有效的 URL 示例?

标签: css ruby-on-rails ruby sinatra


【解决方案1】:

我认为这可能会解决您的问题,我认为您的样式表的链接可能已损坏。 如果页面不在顶部路径中,如果它看起来像这样:

http://localhost/namespace/ThisIsThePage

在您的layout.erb-文件中,您使用以下方式引用样式表:

href="style.css"

在您的 CSS 导入中,我建议您使用例如:

href="/style.css"

注意文件名前的斜线。斜杠告诉浏览器该资源将从您的网络服务器的根路径加载。如果没有斜线,浏览器将尝试从以下路径中获取style.css

http://localhost/namespace/style.css

【讨论】:

  • 这成功了!没有正确链接样式表我觉得有点傻,非常感谢你的帮助!!!! :)
  • 很高兴得到@slope 的帮助,当浏览器尝试从错误的 URL 获取样式表时,它很可能尝试获取 Rails/Sinatra 404 页面作为样式表资源,因此 MIME 类型错误。如果您将答案标记为已接受,我会很高兴(:
猜你喜欢
  • 2014-09-11
  • 2013-12-15
  • 1970-01-01
  • 2011-11-11
  • 2022-08-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多