我正在关注Sinatra :: Base上本教程中的配方 https://www.safaribooksonline.com/library/view/sinatra-up-and/9781449306847/ch04.html
我在尝试使用我的路线时遇到问题,目前只有一条路线正常工作,get '/'
从ApplicationController < Sinatra::Base
home.erb加载。
我的第二个控制器中名为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
答案 0 :(得分:1)
根据教程,您看起来在config.ru文件和ExampleController的路由中都使用了路由/example
。这可能意味着您的本地网址最终可能会被视为&#39; http://localhost:4567/example/example&#39; 。根据事物的外观,您的example_controller.rb文件应该如下所示,使用&#39; /&#39;作为路线:
<强> 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