0

I'm trying to learn Roda and am having a bit of trouble moving into a style of separating out routes into their own files. I got the simple app from the Roda README running and am working on that as a base.

I am trying to use the multi_route plugin as well. So far, this is my setup:

config.ru

require 'rack/unreloader'
Unreloader = Rack::Unreloader.new { Greeter }
require 'roda'
Unreloader.require './backend.rb'
run Unreloader

backend.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'
end

hello.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

So, now when I do my rackup config.ru and go to localhost:9292 in my browser, I get a blank page and 404s in the console. What is out of place? I suspect I'm not using multi_route correctly, but I'm not sure.

4

1 回答 1

0

它有点旧,但也许以后对某人有用。

对于 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/helloand 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
end
于 2018-10-15T12:57:43.543 回答