1

ただし、Rack で基本的なルーティング システムをセットアップしようとしています。最初のルート (「/」) が機能し、2 番目のルート (「/help」) が機能しない理由がわかりません。「/help」の場合に返されるのは NoMethodError です。それはなぜですか、どうすれば修正できますか? ありがとうございました。

require 'rack'

class MyApp
  def self.call(env)
    new.call(env)
  end

  def self.get(path, &block)
    @@routes ||= {}
    @@routes[path] = block
  end

  def call(env)
    dup.call!(env)
  end

  def call!(env)
    @req = Rack::Request.new(env)
    @res = Rack::Response.new
    @res.write route(@req.path)
    @res.finish
  end

  def route(path)
    if @@routes[path]
      @@routes[path].call
    else
      'Not Found'
    end
  end

  def to_do(arg)
    arg
  end
end

MyApp.get '/' do
  'index'
end

MyApp.get '/help' do
  to_do 'help'
end

run MyApp
4

0 に答える 0