Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sinatra application cannot find views directory

Tags:

ruby

view

sinatra

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.

like image 694
Amit Erandole Avatar asked Jul 04 '26 11:07

Amit Erandole


1 Answers

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

like image 67
Amit Erandole Avatar answered Jul 09 '26 10:07

Amit Erandole



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!