我遵循Sinatra::Base上本教程中的配方https://www.safaribooksonline.com/library/view/sinatra-up-and/9781449306847/ch04.html
我的路线无法正常工作,目前只有一条路线可以工作,那就是从ApplicationController < Sinatra::Base
home.erb加载的get '/'
。
我的第二个控制器(名为ExampleController < ApplicationController
)中的路由不工作
config.ru(仅限相关代码)
# Load Controllers and models
Dir.glob('./{controllers,models}/*.rb').each { |file| require file }
map('/example') { run ExampleController }
map('/') { run ApplicationController }
application_controller.rb
class ApplicationController < Sinatra::Base
# set folder for root
set :root, File.expand_path("../app", Dir.pwd)
# don't enable logging when running tests
configure :production, :development do
enable :logging
end
get '/' do
title "Home.erb"
erb :home
end
not_found do
title 'Not Found!'
erb :not_found
end
end
example_controller.rb路由不会从当前加载
class ExampleController < ApplicationController
get '/example' do
title "Example Page"
erb :example
end
end
根据教程,您在config.ru文件和ExampleController的路由中都使用了路由/example
。这可能意味着您的本地url可能会以"http://localhost:4567/example/example"。从外观上看,example_controller.rb文件应该是这样的,路径为"/":
example_controller.rb
class ExampleController < ApplicationController
get '/' do
title "Example Page"
erb :example
end
end
看起来您还需要在config.ru文件中使用require 'sinatra/base'
。
config.ru
# Load Controllers and models
require 'sinatra/base'
Dir.glob('./{helpers,controllers}/*.rb').each { |file| require file }
map('/example') { run ExampleController }
map('/') { run ApplicationController }
此外,您的application_controller.rb似乎缺少帮助程序,并且不包括集合视图。
application_controller.rb
class ApplicationController < Sinatra::Base
helpers ApplicationHelper
# set folder for templates to ../views, but make the path absolute
set :views, File.expand_path('../../views', __FILE__)
# don't enable logging when running tests
configure :production, :development do
enable :logging
end
get '/' do
title "Home.erb"
erb :home
end
# will be used to display 404 error pages
not_found do
title 'Not Found!'
erb :not_found
end
end