ERB engine doesn't working with css link using rack gem

29 Views Asked by At

trying to serve files using rack gem and erb, the css link tag in html files erb is rendering doesn't seens to work:

enter image description here

helper used to execute erb and render view:

def render(file, *args)
    template = File.read("public/#{file}.html.erb") || 
    File.read("public/404.html.erb")
    template_output = ERB.new(template).result(binding)

    res = Rack::Response.new(bind_layout { template_output })
end

def bind_layout(&block)
    layout = File.read("public/layout.html.erb")
    ERB.new(layout).result(binding)
end

layout with css import:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <link rel="stylesheet" href="stylesheets/layout.css">
</head>
<body>
    <div class="main">
        <h1>ONM LAYOUT</h1>
        <%= yield %>
        <h2>onfoototer</h2>
    </div>
</body>
</html>

the page serves the templates and layout correctly, what isn't true for the linked css

1

There are 1 best solutions below

0
wnccys On BEST ANSWER

the problem was that rack was interpreting the file as html, serving all together with the binded html views. The solution I found was use Rack::Static to proper serve the static css as it should: use Rack::Static, :urls => ['/static/stylesheets', 'layout.css'].