cssrubysinatraslim-lang

How to link a css file with the Slim templating engine?


i am working on a little Sinatra/Slim project, and i want to include a css file. All works fine except that.

Here is my project "tree":

├── essai.rb
    └── views
        ├── form.slim
        └── style.css

And this is the head of form.slim:

html

head

    title Vote
    link href="style.css" rel='stylesheet' type='text/css'

I've tried "views/style.css", "../style.css",...etc. but Rake log the error "GET /style.css HTTP/1.1" 404. I know i have no routes for the css in sinatra, but i suppose there is a trick to avoid that... i hope :D

And i tried like i can see on the slim example page: file_path("style.css") but, i have a undefined method 'file_path' error, i guess because its a ruby function passed in args in the slim example.

Could someone tell me how can i do that? Thanks!


Solution

  • Sinatra expects your css files files to reside in the public directory, not within the views directory by default.

    So your project tree should look more like this.

    /essai.rb
    /views
          /form.slim
    /public
          /css
              /style.css
    

    Then you should be able to access it with your existing slim template.