2012-11-10 94 views
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而不是/登录从主应用程序。 我如何强制它做正确的重定向?我知道我可以使用redirect to('../login'),但看起来很糟糕。

回答

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

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喜欢这里。

相关问题