dimanche 12 juin 2016

Sinatra asset pipeline, can't make it work

I am using Sprockets with Sinatra, as suggested in Sinatra's page docs, but I can't make it work.

When I go to localhost:4567, the page loads correctly but with no styles. If I go to localhost:4567/assets/app.css, I get a not found error. I wonder what I am missing or what is wrong in the way I am using Sprockets?

This is my folder structure:

├── assets
│   ├── css
│   │   ├── app.css
│   │   ├── base.css
│   │   └── normalize.css
├── bin
│   └── app
├── lib
│   ├── app_assets.rb
│   └── main.rb
├── spec
│   ├── spec_helper.rb
│   └── main_spec.rb
├── views
│   └── index.erb
├── Gemfile
├── Gemfile.lock
├── Rakefile
├── .rspec
└── .ruby-version

The contents of app.css are:

//= require normalize
//= require base

The contents of app_assets.rb are:

module AppAssets

  def self.environment root_path
    environment = Sprockets::Environment.new root_path
    environment.append_path './assets/css/'
    environment

    # get assets
    get '/assets/*' do
      env['PATH_INFO'].sub!('/assets', '')
      settings.environment.call(env)
    end
  end

end

The contents of lib/main.rb are:

require 'sinatra'
require 'sprockets'
require 'app_assets'

class Main < Sinatra::Base

  set :views, "#{settings.root}/../views"

  get '/' do
    erb :index
  end

end

The file views/index.erb contains the line:

<link rel="stylesheet" href="assets/app.css">

And the contents of bin/app are:

#!/usr/bin/env ruby

$LOAD_PATH.unshift File.join(File.dirname(__FILE__), '..', 'lib')

require 'sinatra'
require 'sprockets'
require 'app_assets'

require 'main'
Main.run!

Which I run typing:

$ bin/app

Any help would be appreciated, I'm sure I made something wrong but I can't see what. Can anybody spot it?

Aucun commentaire:

Enregistrer un commentaire