我在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
答案 0 :(得分:0)
您可以使用splat参数:
get "/:theme/*/?" do
haml "pages/#{params[:theme]}/#{params[:splat].to_s}/index".to_sym
end