0
0

More than 3 years have passed since last update.

NoMethodErrorが出てview、controllerをいじっても解決できない時(備忘録)

Posted at

NoMethodErrorが出た時は、基本的にcontrollerでメソッドが定義できていないかviewとの対応があっていない(タイプミスなど)場合が多い。

ただ、viewやcontrollerを見直してもエラーが消えない時がある。

そんな時は、ネストしたときにmodelで記述する
「has_many」と「belongs_to」で書かれている部分が間違っている可能性がある。

以下に「review」が「hotel」に属しているmodelを例に、注意するべきことをメモに残す。

①has_manyの後ろは複数形でなければならない。

 hotel.rb
class Hotel < ApplicationRecord
    has_many :reviews
end

②belongs_toの後ろは単数系でなければならない。

 review.rb
class Review < ApplicationRecord
    belongs_to :hotel
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