2010-06-07 8 views
1

J'ai besoin de sous-répertoires imbriqués dans mon application sinatra, comment puis-je simplifier ce code répétitif?Comment simplifier mes routes sinatra imbriquées?

# ------------- SUB1 -------------- 
get "/:theme/:sub1/?" do 
    haml :"pages/#{params[:theme]}/#{params[:sub1]}/index" 
end 

# ------------- SUB2 -------------- 
get "/:theme/:sub1/:sub2/?" do 
    haml :"pages/#{params[:theme]}/#{params[:sub1]}/#{params[:sub2]}/index" 
end 

# ------------- SUB3 -------------- 
get "/:theme/:sub1/:sub2/:sub3/?" do 
    haml :"pages/#{params[:theme]}/#{params[:sub1]}/#{params[:sub2]}/#{params[:sub3]}/index" 
end 

# ------------- SUB4 -------------- 
get "/:theme/:sub1/:sub2/:sub3/:sub4/?" do 
    haml :"pages/#{params[:theme]}/#{params[:sub1]}/#{params[:sub2]}/#{params[:sub3]}/#{params[:sub4]}/index" 
end 

Répondre

0

Vous pouvez utiliser les paramètres de floc:

get "/:theme/*/?" do 
    haml "pages/#{params[:theme]}/#{params[:splat].to_s}/index".to_sym 
end 
+0

Merci! Bien que devrait être 'haml:" ' –

+0

La méthode' to_sym' va convertir la chaîne en un symbole.J'ai mis à jour la réponse –

+0

Ah ok, cool, les deux travaillent –