I
I
Ilya2015-09-23 23:31:09
ruby
Ilya, 2015-09-23 23:31:09

How to link to stylesheet file in .erb file?

Having mastered more or less HTML and CSS, I decided to start learning Ruby. While getting acquainted with the sinatra. And the question arose - how to connect a CSS table to a file with ERB resolution? Let's say there is a file ./index.rb

# ./index.rb
require "sinatra"

get "/" do 
  erb :index
end

Also, let's say there is a file ./views/index.erb
<!--  ./views/index.erb -->
<!DOCTYPE html>
<html>
  <head>
    <title>sth</title>
  </head>
  <body>
    <%- 10.times do -%>
      Something;<br>
    <%- end -%>
  </body>
</html>

How can I add a stylesheet file to this file? It turned out only through the built-in table, for example like this:
<!--  ./views/index.erb -->
  <head>
    <title>sth</title>
    <style type="text/css">
      body {
        font-size: 16px;
        font-family: Verdana;
        background-color: rgba(222, 222, 222, 0.6)
      }
    </style>
  </head>

All other connection attempts ended in a fiasco.
# Yes, the question is probably stupid and the answer will be obvious.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
J
Jeiwan, 2015-09-24
@Harmn

Sinatra by default gives statics from the public folder. You need to create this folder (if it doesn't exist), create a css folder in it and include the style like this:
(note that there is no public in the style path)
JS scripts are also included.
PS At the same time, I recommend the site https://mkdev.me/ There is a course on the basics of ruby, on working with rails, as well as a free book-guide to web development. They also offer mentoring services.

R
Roman Vakulchik, 2015-09-24
@rvakulchik

You can try to connect as in normal HTML

<head>
  <link rel="stylesheet" type="text/css" href="theme.css">
</head>

Purely for erb:
<%= stylesheet_link_tag 'theme' %>

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question