こんにちは。
表題の件です。
note.rbでbelongs_to :userをしたのにも関わらず、
noteクラス内でname(userクラスのメソッド)が使えません。
みなさんの指摘お待ちしてます!!
コードは以下のようになっています。
user.rb
class User < ActiveRecord::Base
has_many :notes
validates :name, presence: true
validates :email, presence: true, uniqueness: true
end
note.rb
class Note < ActiveRecord::Base
★belongs_to :user★
validates :title, presence: true
validates :content, presence: true, length: {maximum: 140 }
validates :user_id, presence: true
end
_note.html.erb
<li> <span class="user-name"> <%= note.user.name %> </span> <span class="content"> <%= link_to note.title, note_path(note.id) %> </span> </li>index.html.erb
<h1>投稿の一覧</h1> <ul class="notes"> <% @notes.each do |note| %> <%= render 'notes/note', note: note %> <% end %> </ul>表示されるエラー
NoMethodError in Notes#index
Showing C:/Users/keiga/Desktop/rails/Groovy/app/views/notes/_note.html.erb where line #3 raised:
undefined method `name' for nil:NilClass
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。