我正在尝试创建一个模块化的sinatra应用程序,并需要我的每个子应用程序在项目文件夹的根目录中查找views
目录。但它只查找子目录本身的视图目录而不是根目录。这是我的项目的样子:
├── 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
所以在我的config.ru中我尝试这样做:
require 'sinatra'
require './music_catalog/app.rb'
set :root, File.dirname(__FILE__)
# enable :run
map "/" do
run MusicCatalog
end
在app.rb
内music_catalog
我使用根变量,如下所示:
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
但不是从我的根目录中取出index.haml
,而是错误地输出:
Errno::ENOENT at /
No such file or directory - /Users/amiterandole/Dropbox/code/rsandbox/sinatra_music_store/music_catalog/views/index.haml
我正在使用ruby 1.9.3p194
请帮我将views目录设置为根views
文件夹中的正确位置。
答案 0 :(得分:5)
好的我明白了。 set:views语句实际上应该在我的app类中,如下所示:
class MusicCatalog < Sinatra::Base
**set :views, Proc.new { File.join(root, "../views") }**
get "/" do
haml :index
end
end
此外,我之前以错误的方式加入了根。修正了。现在sinatra正确加载我的模板