如何在 Sinatra 中共享错误和 not_found 处理程序

发布于 2024-12-15 13:27:07 字数 392 浏览 1 评论 0原文

我正在使用 Ruby 和 Sinatra 创建一个 Web 应用程序,并将各个方面拆分为单独的 Sinatra::Base 类,如下所示:

class Frontend < Sinatra::Base
  get '/' do
    erb :home
  end
end

class Backend < Sinatra::Base
  get '/account' do
    erb :account
  end
end

现在我想使用 not_found 和 error 路由,但我不想在两个类中重复它们。

声明一次并将它们应用于两个类中的路由的最佳方法是什么?

I am creating a web app using Ruby and Sinatra, and I'm splitting up the various aspects into separate Sinatra::Base classes, like so:

class Frontend < Sinatra::Base
  get '/' do
    erb :home
  end
end

class Backend < Sinatra::Base
  get '/account' do
    erb :account
  end
end

Now I want to use the not_found and error routes, but I don't want to duplicate them in both classes.

What's the best way to declare them once and have them apply to routes in both classes?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

笑梦风尘 2024-12-22 13:27:07
class SomeAwesomeClassName < Sinatra::Base
  get '/not_found' do
  end

  get '/error' do
  end
end

class MyApp < Sinatra::Base
  use SomeAwesomeClassName

  get '/' do
  end
end
class SomeAwesomeClassName < Sinatra::Base
  get '/not_found' do
  end

  get '/error' do
  end
end

class MyApp < Sinatra::Base
  use SomeAwesomeClassName

  get '/' do
  end
end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文