LoginSignup
1
0

More than 1 year has passed since last update.

【rails】ルートにposts/indexを指定している状態で、posts/indexをURLに打ち込んだらエラーが起きた。その原因と解決策

Posted at

状況

  • localhost:3000/posts/indexにアクセスしたら以下のエラー
    スクリーンショット 2022-03-09 20.31.13.png

原因

  • ルーティングにposts/indexが指定されていなかった

原因に気付かなかった理由

  • rootにposts/indexを指定していたからそれで十分だと思っていた
  • resouces:postsがあるからそれで十分だと思っていた
routes.rb
Rails.application.routes.draw do
  devise_for :users,
    controllers: { registrations: 'registrations' }
    
    root'posts#index'
    get "posts/index" => "posts#index"

    get "users/index" => "users#index"
    get '/users/:id', to: 'users#show', as: 'user'

  #親子関係にあるということ
  resources :posts, only: %i(index new create show destroy) do
    resources :photos, only: %i(create)
    resources :likes, only: %i(create destroy)
    resources :comments, only: %i(create destroy)
  end
  
end
get "posts/index" => "posts#index"

この部分を追加したらposts/indexで検索してもページがちゃんと表示された .

  • posts使ってたから十分だと思ってたけど、これは親子関係での指定だからシンプルなresouces とは違ってたって認識でいいのかな?
    また後日調べます
1
0
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
1
0