[英]Configuring erb directory in Sinatra
应用程序/控制器/ app.rb
require 'sinatra'
get '/' do
erb :index
end
应用/视图/ index.erb
<html>
<body>
<p>Hello World</p>
</body>
</html>
错误:
Errno::ENOENT at /
No such file or directory - .../app/controllers/views/index.erb
如何配置erb以查看app/views
而不是app/controllers/views
?
您可以通过调整配置设置来实现此目的。 由于您使用的是非标准设置,因此您需要告诉Sinatra应用程序的实际根目录以及查找视图的位置。 在app/controllers/app.rb
文件的顶部添加:
# sets root as the parent-directory of the current file
set :root, File.join(File.dirname(__FILE__), '..')
# sets the view directory correctly
set :views, Proc.new { File.join(root, "views") }
您可以在Sinatra文档中阅读有关Sinatra 配置选项的更多信息。
set :views, Proc.new { File.join(root, "views") }
来自http://www.sinatrarb.com/configuration.html#__view_template_directory
编辑:显然,什么都不做,嘿。 在app/
中需要控制器的文件可能是最好的:
Dir.glob("controllers/*.rb").each { |r| require_relative r }
然后, app/views
将成为默认的views目录。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.