0
0

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 3 years have passed since last update.

NameErorrのエラー出現

Posted at

今日の備忘録、というか完全に初歩的なミス
※前提として、この時deviseで作成したAdminのためのマイページを作成するため、専用のAdminsコントローラーとviewを作ってました。ルーティングはAdminとUserの複数モデルを管理していたため、namespaceで分けて記載。

NameErorrのエラーが発生し、クラスの名前や指定するルーティングをミスったのかと思い何度も確認。

全く間違いぽいものは見つからず、その時は確認すらしなかったroutes.rbにしっかりミスが。

routes.rbを確認すると、

/config/routes.rb
Rails.application.routes.draw do

  devise_for :admins, controllers: {
    sessions:      'admins/sessions',
    passwords:     'admins/passwords',
    registrations: 'admins/registrations'
  }
  devise_for :users, controllers: {
    sessions:      'users/sessions',
    passwords:     'users/passwords',
    registrations: 'users/registrations'
  }

  namespace :admins do
    root 'post_images#index'
    resources :post_images, only: [:new, :create, :index, :show, :destroy]
  end

  scope module: 'users' do
    root 'home#top'
    resources :users, only: [:show, :edit, :update]
    resources :post_images, only: [:new, :create, :index, :show, :destroy] do
      resources :post_comments, only: [:create, :destroy]
    end
  end
end

namespace :adminsの中に書いていると勝手に思い込んでいたresources :adminsが書いてない、てか書いた痕跡もない。

早急に修正

/config/routes.rb
Rails.application.routes.draw do

  devise_for :admins, controllers: {
    sessions:      'admins/sessions',
    passwords:     'admins/passwords',
    registrations: 'admins/registrations'
  }
  devise_for :users, controllers: {
    sessions:      'users/sessions',
    passwords:     'users/passwords',
    registrations: 'users/registrations'
  }

  namespace :admins do
    root 'post_images#index'
    resources :post_images, only: [:new, :create, :index, :show, :destroy]
    resources :admins, only: [:show, :edit, :update]
  end

  scope module: 'users' do
    root 'home#top'
    resources :users, only: [:show, :edit, :update]
    resources :post_images, only: [:new, :create, :index, :show, :destroy] do
      resources :post_comments, only: [:create, :destroy]
    end
  end
end

誰もこんな初歩的なミスはしないと思うのですがもし誰かの参考にでもなれば嬉しいです。

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?