问题
My stylesheet is applying to some elements of my app and not others, and I'm not sure why.
Here is my document structure:

Currently, the stylesheet is being applied to index.erb and contacts.erb, but not the others in the tree, and I'm not sure why.
In my layout I've specified: <link rel="stylesheet" type="text/css" href="style.css">
Any ideas?
回答1:
You are using a relative reference to your stylesheet, so the browser will request it relative to the current url. This means for /
and /contacts
the browser will request /styles.css
and so find it, but for example /contacts/1
it will request /contacts/style.css
which doesn’t exist. If you check your logs you’ll likely see a load of 404s where this has happened.
The solution is to use an absolute path for your stylesheet. Assuming you app is mounted at the root of you server (i.e. requests are made to e.g. http://example.com/contacts/1
) you can do this by just adding a /
to the beginning of the href
:
<link rel="stylesheet" type="text/css" href="/style.css">
A more robust solution would be to use the url helper, which will ensure the correct url will be used even if your app is mounted at another point (e.g. within a Rails app) or is behind a proxy:
<link rel="stylesheet" type="text/css" href="<%= url("/style.css")%>">
回答2:
<link rel="stylesheet" type="text/css" href="/style.css">
the public folder is always taken to store the css,js files it will be store in the cache of the browser once you are in the production environment
来源:https://stackoverflow.com/questions/24153013/stylesheet-not-applying-to-sinatra-erb-views