htmlrubysinatraasset-pipelinenomethoderror

Sinatra NoMethodError at / undefined method `stylesheet_link_tag' for #


I am currently making an app with sinatra and i've been working for hours to try to implement bootstrap in it, and realizing how much easier it is in rails.

Anyways, when i run the web server and go to /, I see an error relating to the line <%= stylesheet_link_tag 'app', media: 'all' %> which I put in my app.erb view(the index of my webapp). This line is meant to load an scss file in the assets/stylesheets folder, and the file contains the @import values for bootstrap.

I'm using the sinatra-asset-pipeline gem, and I've set it up correctly according to the instructions on the github page. I've been trying lots of things for the past 4 hours and nothing has worked. Here's the code for the most important files:

assets/stylesheets/app.scss:

@import "bootstrap-sprockets";
@import "bootstrap";

views/app.erb:

<!DOCTYPE html>
<html>

  <head>
    <title>Salah Kayali</title>
    <%= stylesheet_link_tag 'app', media: 'all' %>
    <%= javascript_include_tag 'app' %>
    <meta name="viewport" content="width=device-width, initial-scale=1">
  </head>

  <body>
    <%= partial :header %>
  </body>

</html>

app.rb:

require 'sinatra/partial'
require 'rubygems'
require 'sinatra'
# set :bind, '0.0.0.0'
require 'sinatra/asset_pipeline'

class App < Sinatra::Base
    register Sinatra::Partial
    configure do
    set :assets_precompile, %w(app.js app.scss *.png *.jpg *.svg *.eot *.ttf *.woff)
    set :assets_css_compressor, :sass
    set :assets_js_compressor, :uglifier
        register Sinatra::AssetPipeline

        if defined?(RailsAssets)
      RailsAssets.load_paths.each do |path|
        settings.sprockets.append_path(path)
      end
    end
  end

  enable :partial_underscores
    set :partial_template_engine, :erb

  get '/' do
    erb :app
  end
  run! if __FILE__ == $0
end

config.ru:

require 'rubygems'
require 'bundler'
Bundler.require
run App

Rakefile:

require 'sinatra/asset_pipeline/task'
require './app'

Sinatra::AssetPipeline::Task.define! App

Gemfile:

source "https://rubygems.org"
ruby "2.2.3"

gem 'bundler', '~> 1.10.6'

gem 'sinatra', require: 'sinatra/base'
gem 'sinatra-asset-pipeline', require: 'sinatra/asset_pipeline'

gem 'thin', '~> 1.6.4'

gem 'sass', '~> 3.4.18'

gem 'coffee-script', '~> 2.4.1'

gem 'uglifier', '~> 2.7.2'

gem 'rack-coffee', '~> 1.0.3'

gem 'sinatra-partial'

source 'https://rails-assets.org' do
  gem 'rails-assets-jquery'
  gem 'rails-assets-bootstrap-sass'
end

I hope someone can help, as I really don't know what to do. I'm normally used to rails, where the asset-pipeline already exists and takes care of everything. I'm forcing myself to make this webapp with sinatra, though, so that I can really understand what actually happens when you "automagically" create things with rails.

Here is a link to a screenshot of the error output in my browser: http://puu.sh/ktUIA/1d8396b034.png


Solution

  • Your problem is that stylesheet_link_tag and javascript_include_tag are a part of ActionView, one of the gems underlying Rails (i.e. see ActionView::Helpers::AssetTagHelper).

    According to the sinatra-asset-pipeline documentation:

    Now when everything is in place you can use all helpers provided by sprockets-helpers

    Looking at a sprocket-helpers example you need to do something like this:

    <link rel="stylesheet" href="<%= stylesheet_path 'application' %>">
    <script src="<%= javascript_path 'application' %>"></script>
    

    So what you're looking for is stylesheet_path and javascript_path rather than stylesheet_link_tag and javascript_include_tag.