Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sinatra cannot find views directory

I'm trying to structure my Sinatra app in a way that's more similar to the normal Ruby Gem structure. I have the following file tree:

.
├── app.rb
├── config.ru
├── Gemfile
├── Gemfile.lock
├── helpers
│   ├── dbconfig.rb
│   ├── functions.rb
│   └── init.rb
├── hidden
│   └── Rakefile
├── lib
│   ├── admin.rb
│   ├── api.rb
│   ├── indexer.rb
│   ├── init.rb
│   └── magnet.rb
├── models
│   ├── init.rb
│   ├── invite.rb
│   ├── tag.rb
│   ├── torrent.rb
│   └── user.rb
├── public
│   ├── css
│   │   ├── reset.css
│   │   └── style.css
│   ├── i
│   ├── img
│   │   ├── bg.jpg
│   │   ├── dl-icon.png
│   │   ├── logo.png
│   │   ├── logo-public.png
│   │   ├── magnet-icon.png
│   │   ├── text-logo.png
│   │   ├── text-logo-public.png
│   │   └── upload-icon.png
│   └── js
│       ├── main.js
│       └── torrents.js
├── README.md
├── SPEC.md
├── tmp
│   └── restart.txt
├── TODO.md
└── views
    ├── error.erb
    ├── footer.erb
    ├── header.erb
    ├── index.erb
    ├── list.erb
    ├── nav.erb
    ├── text.erb
    └── upload.erb

I have the app files that are trying to render things in lib/, but upon (re)starting the Passenger server I get: Errno::ENOENT - No such file or directory - /home/dev/indexer/lib/views/index.erb

The offending few lines are:

get '/' do
  erb :index
end

How can I fix this?

like image 677
tekknolagi Avatar asked Jan 21 '13 07:01

tekknolagi


2 Answers

Without looking at the content of rb files it would be difficult to guess what is wrong. If you routes are defined in rb files in sub-folder within root of your application, you would have to explicitly set views folder.

In your case, assuming /app.rb is the file setting up the basic configuration, you would have to set views (or anything else like public folder) in there.

class MyApp < Sinatra::Base
  set :views, File.dirname(__FILE__) + '/views'
  set :public_folder, File.dirname(__FILE__) + '/public'
end

Else Sinatra would look in to subsequent sub-folders for relevant paths. Further, subsequent route files should extend same class. In this case MyApp. For example, in ./lib/admin.rb

class MyApp < Sinatra::Base
  get "/blah" do
    "blah blah"
  end
end
like image 116
ch4nd4n Avatar answered Sep 23 '22 02:09

ch4nd4n


Specify path to views directory in your configuration block:

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

See http://www.sinatrarb.com/configuration.html#__view_template_directory

like image 37
NARKOZ Avatar answered Sep 22 '22 02:09

NARKOZ