如何简化嵌套的sinatra路线?

时间:2010-06-07 14:28:30

标签: ruby routing sinatra

我在sinatra应用程序中需要嵌套子目录,如何简化这些重复代码?

# ------------- 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

1 个答案:

答案 0 :(得分:0)

您可以使用splat参数:

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