Joe Half Face Joe Half Face - 7 months ago 16
Ruby Question

Sinatra doesn't load assets

I have a sinatra landing page. Server starts by execution

index.rb
in root folder.

For example:

get '/en' do
I18n.locale = 'en'
erb :index, locals: {langswitch: "/ru", current_locale: "en"}
end


renders
index.erb
and works well.

This page has:

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


And if I open this link, it points to
http://0.0.0.0:9292/index.css
, retrieves css file and renders page as expected.

However, there is also

get '/agreement/en' do
I18n.locale = 'en'
erb :agreement, locals: {langswitch: "/ru", current_locale: "en"}
end


If I visit the same link from this page, it instead leads to
http://0.0.0.0:9292/agreement/bootstrap.css
, instead of
http://0.0.0.0:9292/index.css
and fails to retrieve file.

Same with all of my images, other css files.

Answer

First you need to put all your assets in public folder. Then you need to modifie <link rel="stylesheet" type="text/css" href="index.css"> to <link rel="stylesheet" type="text/css" href="/index.css">. So all you need to do is to put / before index.css