我正在尝试学习Roda,但在进入一种将路由分离到自己的文件中的风格时遇到了一些麻烦。我从Roda自述文件中运行了这个简单的应用程序,并将其作为基础工作。
我也在尝试使用multi_route插件。到目前为止,这是我的设置:
config.ru
require 'rack/unreloader'
Unreloader = Rack::Unreloader.new { Greeter }
require 'roda'
Unreloader.require './backend.rb'
run Unreloaderbackend.rb
require 'roda'
# Class Roda app
class Greeter < Roda
puts 'hey'
plugin :multi_route
puts 'hey hey hey'
route(&:multi_route)
end
Dir['./routes/*.rb'].each { |f| require f }index.rb
Greeter.route '/' do |r|
r.redirect 'hello'
endhello.rb
Greeter.route '/hello' do |r|
r.on 'hello' do
puts 'hello'
@greeting = 'helloooooooo'
r.get 'world' do
@greeting = 'hola'
"#{@greeting} world!"
end
r.is do
r.get do
"#{@greeting}!"
end
r.post do
puts "Someone said #{@greeting}!"
r.redirect
end
end
end
end因此,现在当我执行rackup config.ru并在浏览器中转到localhost:9292时,我在控制台中得到一个空白页面和404。什么是不合适的?我怀疑我没有正确使用multi_route,但我不确定。
发布于 2018-10-15 20:57:44
它有点老了,但也许以后会对某些人有用。
对于Roda来说,正斜杠意义重大。在您的示例中,Greeter.route '/hello' do |r|与localhost:9292//hello匹配,而不与localhost:9292/hello匹配。
您可能需要以下内容:
Greeter.route do |r|
r.redirect 'hello'
end还有这个。嵌套的r.on 'hello' do意味着树的那个分支只匹配localhost:9292/hello/hello和localhost:9292/hello/hello/world,这可能不是您想要的。
Greeter.route 'hello' do |r|
puts 'hello'
@greeting = 'helloooooooo'
r.get 'world' do
@greeting = 'hola'
"#{@greeting} world!"
end
r.is do
r.get do
"#{@greeting}!"
end
r.post do
puts "Someone said #{@greeting}!"
r.redirect
end
end
endhttps://stackoverflow.com/questions/51715264
复制相似问题