Sinatra application cannot find views directory

ruby, sinatra, view

Solution

Ok I figured it out. The set :views statement should actually be inside my app class like this:

class MusicCatalog < Sinatra::Base

  **set :views, Proc.new { File.join(root, "../views") }**

  get "/" do
    haml :index
  end
end

Also I was joining the root in the wrong way earlier. Fixed that. Now sinatra is correctly loading my templates

Problem

I am trying to create a modular sinatra application and need each of my sub-applications to look for the `views` directory at the root of my project's folder. But it only looks up the views directory in the sub-directory itself instead of the root. Here's what my project looks like: ``` ├── config.ru ├── music_catalog │   └── app.rb ├── public │   ├── css │   │   └── site.css │   └── images │   ├── content_top_bg.jpg │   ├── demo_image_01.jpg │   ├── god_save_http_it_aint_no_human_being.png │   ├── header_bg.jpg │   ├── home-showcase.png │   ├── hover_link_bg.jpg │   ├── its_little_its_blue_and_its_magical.jpeg │   ├── linkbar_bg.jpg │   ├── logo.png │   ├── main_graphics.jpg │   ├── placeholder.gif │   ├── placeholder.jpg │   ├── placeholder.png │   ├── right_navbar_bg.jpg │   └── shadow_left.jpg └── views ├── album.haml ├── genre.haml ├── index.haml ├── layout.haml ├── login.haml └── not_found.haml ``` So in my config.ru I try doing this: ``` require 'sinatra' require './music_catalog/app.rb' set :root, File.dirname(__FILE__) # enable :run map "/" do run MusicCatalog end ``` In `app.rb` inside of `music_catalog` I use the root variable like so: ``` require 'sinatra/base' `# I thing I am doing this wrong` set :views, Proc.new { File.join(root, "sites/#{site}/views") } class MusicCatalog < Sinatra::Base get "/" do haml :index end end ``` But instead of pulling out my `index.haml` out of my root directory, it errors out like so: ``` Errno::ENOENT at / No such file or directory - /Users/amiterandole/Dropbox/code/rsandbox/sinatra_music_store/music_catalog/views/index.haml ``` I am using `ruby 1.9.3p194` Please help me set the views directory to the proper location in the root `views` folder.

Original source