0

我有 Sinatra 基础应用程序,例如:

class MyApp < Sinatra::Base
  get '/' do
    ..
  end
  get '/login' do
    ..
  end
end

和一些子模块,如

class Protected < MyApp

  before '/*' do
    redirect('/login') unless logged_in
  end

  get '/list' do
    ...
  end
end

我的 config.ru 如下所示

map "/" do
  run MyApp
end

map "/protected" do
  run Protected
end

尝试访问时出现重定向循环,/protected/list因为它尝试从主应用程序重定向到/protected/login而不是/login 。我怎样才能强迫它做正确的重定向?我知道我可以使用redirect to('../login'),但它似乎很糟糕。

4

2 回答 2

2

imo 使用 Sinatra,您只能将 URL 分配给常量,然后引用它们。

喜欢:

MAIN_URL = '/'
PROTECTED_URL = '/protected'

class Protected < MyApp

before '/*' do
  redirect( MAIN_URL + 'login') unless logged_in
end

get '/list' do
  ...
end

map MAIN_URL do
  run MyApp
end

map PROTECTED_URL do
  run Protected
end

够丑的。

我建议改用Espresso

在路由以及其他框架糟糕的其他方面非常明智。

路由部分在这里:http ://e.github.com/Routing.html

于 2012-11-10T18:10:24.327 回答
0

从 /config/application.rb 中的我的一个应用程序

class MyApp < Sinatra::Base

  configure do

    APP_ROOT = Pathname.new(File.expand_path('../../', __FILE__))
    # By default, Sinatra assumes that the root is the file that
    # calls the configure block.
    # Since this is not the case for us, we set it manually.
    set :root, APP_ROOT.to_path
    ...

您还可以在 config.ru 中定义该常量,就像这里的第一个答案一样。

于 2015-09-12T17:19:07.407 回答