8
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

ネストしたコントローラーに対してrootにルーティングを設定する

Posted at

今回次のような形でプロジェクト内にコントローラー、ビューが配置されている。

proto_project
proto_project
 /app
  /controllers
   /prototypes
    latest_controller.rb
    ranking_controller.rb

  /views
   /prototypes
    /latest
     index.html.haml
    /ranking

変更前

routes.rb
Rails.application.routes.draw do
  namespace :prototypes do
    resources :ranking, only: [:index]
    resources :latest, only: [:index]
  end
    
end

develop環境にてlocalhost:3000/にアクセスした時にlatest#indexアクションを起動するようにしたい。次の様に書くことでルーティングを設定する。

変更後

routes.rb
Rails.application.routes.draw do
  root to:  'prototypes/latest#index'  

  namespace :prototypes do
    resources :ranking ,only: [:index]
  end 
end

ここで次のように/controllers/prototypes/latest_controller.rbと階層があるので上記のコードのようにroot to: 'prototypes/latest#index' と書くことで期待したルーティングを設定することができた。

8
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
8
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?