我正在尝试为Sinatra应用程序实现命名空间(带有 sinatra-contrib gem )路由,并在我尝试启动服务器后出错:
undefined method `namespace' for GuessApp:Class
app.rb
require 'sinatra'
require 'sinatra/namespace'
require 'haml'
require_relative 'routes/init'
require_relative 'models/init'
require_relative 'helpers/init'
class GuessApp < Sinatra::Base
register Sinatra::Namespace
end
路由/ init.rb
require_relative "books"
require_relative "stars"
路由/ books.rb
class GuessApp < Sinatra::Base
namespace '/books' do
@title = "Books"
get '' do
@books = Book.all
haml :books, layout: :layout
end
end
end
没有名称空间这条路线可以正常工作。
答案 0 :(得分:2)
您需要require 'sinatra/namespace'
之前books.rb
,但之后register Sinatra::Namespace
app 中只有GuessApp
才需要Sinatra::Namespace
。单独要求只能在经典风格的应用程序中使用。
在尝试使用GuessApp
之前,您需要确保在namespace
中注册了app.rb
,如require 'sinatra'
require 'sinatra/namespace'
require 'haml'
class GuessApp < Sinatra::Base
register Sinatra::Namespace
end
require_relative 'routes/init'
require_relative 'models/init'
require_relative 'helpers/init'
中所示:
.appear span {
display: none;
-moz-border-radius: 10px;
-webkit-border-radius: 10px;
border-radius: 10px;
background-color: #000000;
}
.appear:hover span {
display: block;
position: absolute;
margin: 0px 0px 0px 0px;
padding: 0px 0px 0px 0px;
z-index: 1000;
top: 500px;
left: 0px;
width: 544px;
height: 306px;
border: 1px solid #000000;
text-decoration: none;
overflow: hidden;
}
table.pic_matrix {
clear: both;
margin: 0px 0px 0px 10px;
padding: 0px 0px 0px 0px;
width: auto;
height: auto;
text-align: center;
border: none;
border-collapse: collapse;
}
td.pics {
margin: 0px 0px 0px 0px;
padding: 5px 5px 5px 5px;
width: 180px;
height: auto;
text-align: center;
vertical-align: top;
background-color: transparent;
border: none;
color: #ffffff;
}