2012-11-10 98 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喜歡這裏。

相關問題