Qiita Conference 2025

tenntenn (@tenntenn)

好奇心を原動力に行動するソフトウェアエンジニアになるために

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.

【Rails基礎】画面ロード時のエラーについて(NameError, LoadError)

Last updated at Posted at 2020-05-17

2回目の投稿になります。よろしくお願いします。
今回は個人アプリを作る中で起きた画面ロード時に起きたエラーについての記事を書きたいと思います。

環境

Ruby: 2.5.1
RubyOnRais: 5.0.7

エラー内容

エラー内容は以下の二つです。

① LoadError in EventsController#choise_artist
Unable to autoload constant Set_list, expected ~ FesLive-app/app/models/set_list.rb to define it.

② Name Error in EventsController#choise_artist
uninitialized constant EventsController:Setlist

解決方法

①の解決方法

まずひとつ目のエラーですが、autoloadできないと言われています。
まず、Railsのautoloadとは「命名規則に則ったファイルを自動でrequireしてくれる機能」のことです。
つまり、下の記述の @set_lists = Set_list.all は命名規則に従っていないということになります。
実はrails の命名規則ではクラス名に対してアンダーバー(_)を使うことはできません。
そのため、Set_listクラスをautoloadできませんよ〜と言われているのでした。

class EventsController < ApplicationController
  
  def choise_artist
    @set_lists = Set_list.all
    @event = Event.find(params[:id])
  end
  
end

なので、「@set_lists = Setlist.all」とすれば①のエラーに関しては突破できました。

【参考】エラー画面
LoadError in EventsController#choise_artistのエラー.png

②の解決方法

クラス名の表記の仕方を変えautoloadはできるようになりましたが、今度はNameErrorのエラーがでました。
uninitialized ⇒ 初期化されていない ⇒ クラスが使える状態が整っていないということなので、
クラス名の指定の仕方に問題がありそうだということが分かります。
実際にSetlistモデルを見に行くと、クラス名が「SetList」とLが大文字になっていました。
なので、クラス名がまちがってますよ〜と言われているのでした。

class SetList < ApplicationRecord
  belongs_to :event
end
この@set_lists = SetList.allとすればこのエラーは突破できました

【参考】エラー画面
uninitialized constant EventsController--Setlist.png

以上、ここまで読んでくださりありがとうございました。
分かりにくい箇所やアドバイス等ありましたら、コメントくださると幸いです。では!

【参考サイト】
https://qiita.com/hirokisoccer/items/4ba62a56b18eb834a8ee
https://wa3.i-3-i.info/word16120.html

0
0
1

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

Qiita Conference 2025 will be held!: 4/23(wed) - 4/25(Fri)

Qiita Conference is the largest tech conference in Qiita!

Keynote Speaker

ymrl、Masanobu Naruse, Takeshi Kano, Junichi Ito, uhyo, Hiroshi Tokumaru, MinoDriven, Minorun, Hiroyuki Sakuraba, tenntenn, drken, konifar

View event details
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?