0

以下のケースroutes.rbを簡単に表現したいと思います。

 namespace :post do
    namespace :p01 do
      resources :p9_posts
    end
  end
  namespace :post do
    namespace :p01 do
      resources :p8_posts
    end
  end
  namespace :post do
    namespace :p01 do
      resources :p7_posts
    end
  end
  namespace :post do
    namespace :p01 do
      resources :p6_posts
    end
  end
  namespace :post do
    namespace :p01 do
      resources :p5_posts
    end
  end
  namespace :post do
    namespace :p01 do
      resources :p4_posts
    end
  end
  namespace :post do
    namespace :p01 do
      resources :p3_posts
    end
  end
  namespace :post do
    namespace :p01 do
      resources :p2_posts
    end
  end
  namespace :post do
    namespace :p01 do
      resources :p1_posts
    end
  end
  namespace :post do
    namespace :p01 do
      resources :p0_posts
    end
  end

ループを使用する簡単な式を作成したいのですが、どうすればそれができますか

そしてまた下にも

  namespace :post do
    namespace :p10 do
      resources :posts do
        resources :comments
      end
    end
  end
  namespace :post do
    namespace :p09 do
      resources :posts do
        resources :comments
      end
    end
  end
  namespace :post do
    namespace :p08 do
      resources :posts do
        resources :comments
      end
    end
  end
  namespace :post do
    namespace :p07 do
      resources :posts do
        resources :comments
      end
    end
  end
  namespace :post do
    namespace :p06 do
      resources :posts do
        resources :comments
      end
    end
  end
  namespace :post do
    namespace :p05 do
      resources :posts do
        resources :comments
      end
    end
  end
  namespace :post do
    namespace :p04 do
      resources :posts do
        resources :comments
      end
    end
  end
  namespace :post do
    namespace :p03 do
      resources :posts do
        resources :comments
      end
    end
  end
  namespace :post do
    namespace :p02 do
      resources :posts do
        resources :comments
      end
    end
  end
  namespace :post do
    namespace :p01 do
      resources :posts do
        resources :comments
      end
    end
  end
4

1 に答える 1

0

次のように書き直すことができます。

namespace :post do
  namespace :p01 do
    9.downto(0) { |i| resources "p#{i}_posts".to_sym }
  end
end
于 2013-06-06T07:06:54.753 に答える