[RUBY] [Rails basics] About errors when loading screens (NameError, LoadError)

This is the second post. Thank you. This time I would like to write an article about the error that occurred when loading the screen that occurred while making a personal application.

environment

Ruby: 2.5.1 RubyOnRais: 5.0.7

error contents

The error contents are the following two.

① 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

Solution

Solution of ①

The first error is that it cannot be autoloaded. First of all, Rails autoload is a "function that automatically requires files that follow naming conventions". This means that @set_lists = Set_list.all in the description below does not follow the naming convention. In fact, rails naming conventions don't allow underscores (_) for class names. Therefore, I was told that the Set_list class cannot be autoloaded.

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

So, if you set "@set_lists = Setlist.all", you could break through the error of ①.

[Reference] Error screen LoadError in EventsController#choise_artistのエラー.png

(2) Solution

I changed the notation of the class name so that autoload can be done, but this time I got a NameError error. uninitialized ⇒ uninitialized ⇒ because the class is not ready for use You can see that there seems to be a problem with how to specify the class name. When I actually went to see the Setlist model, the class name was "SetList" and L was capitalized. So I was told that the class name was wrong ~.

class SetList < ApplicationRecord
  belongs_to :event
end
this"@set_lists = SetList.If you say "all", you can break through this error.

[Reference] Error screen uninitialized constant EventsController--Setlist.png

Thank you for reading this far. If you have any questions or advice that are difficult to understand, please comment. Then!

[Reference site] https://qiita.com/hirokisoccer/items/4ba62a56b18eb834a8ee https://wa3.i-3-i.info/word16120.html

Recommended Posts

[Rails basics] About errors when loading screens (NameError, LoadError)
[Rails] About collection when rendering partial (when loading partial template)
How to resolve errors when installing Rails 5.1.3
Rails basics
About Rails 6
[Ruby On Rails] About RubyAws :: Sigv4 :: Errors :: MissingCredentialsError