発生している問題・エラーメッセージ
・MicropostモデルとUserモデルを紐づけたが、userメソッドが使えない
↓がエラーメッセージです。
NoMethodError in Microposts#index Showing /home/ubuntu/environment/rails_app/app/views/microposts/_micropost.html.erb where line #3 raised: undefined method `user' for #<Class:0x00007fab78d47418> Did you mean? super app/views/microposts/_micropost.html.erb:3:in `_app_views_microposts__micropost_html_erb__4579145426954223205_70304152209200' app/views/microposts/index.html.erb:2:in `block in _app_views_microposts_index_html_erb___1913828141426248439_70304152043480' app/views/microposts/index.html.erb:1:in `_app_views_microposts_index_html_erb___1913828141426248439_70304152043480'
ActionView::Template::Error (undefined method `user' for #<Class:0x0000565271879460> Did you mean? super): 1: <% micropost = Micropost %> 2: <li id="micropost-<%= micropost.ids %>"> 3: <%= link_to gravatar_for(micropost.user, size: 50), micropost.user %> 4: <span class="user"><%= link_to micropost.user.name, micropost.user %></span> 5: <span class="content"><%= micropost.content %></span> 6: <span class="timestamp"> app/views/microposts/_micropost.html.erb:3:in `_app_views_microposts__micropost_html_erb__4579145426954223205_70304152209200' app/views/microposts/index.html.erb:2:in `block in _app_views_microposts_index_html_erb___1913828141426248439_70304152043480' app/views/microposts/index.html.erb:1:in `_app_views_microposts_index_html_erb___1913828141426248439_70304152043480' Rendering /home/ubuntu/.rvm/gems/ruby-2.6.3/gems/actionpack-5.0.7.2/lib/action_dispatch/middleware/templates/rescues/template_error.html.erb within rescues/layout Rendering /home/ubuntu/.rvm/gems/ruby-2.6.3/gems/actionpack-5.0.7.2/lib/action_dispatch/middleware/templates/rescues/_source.html.erb Rendered /home/ubuntu/.rvm/gems/ruby-2.6.3/gems/actionpack-5.0.7.2/lib/action_dispatch/middleware/templates/rescues/_source.html.erb (3.8ms) Rendering /home/ubuntu/.rvm/gems/ruby-2.6.3/gems/actionpack-5.0.7.2/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb Rendered /home/ubuntu/.rvm/gems/ruby-2.6.3/gems/actionpack-5.0.7.2/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb (1.6ms) Rendering /home/ubuntu/.rvm/gems/ruby-2.6.3/gems/actionpack-5.0.7.2/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb Rendered /home/ubuntu/.rvm/gems/ruby-2.6.3/gems/actionpack-5.0.7.2/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb (0.6ms) Rendered /home/ubuntu/.rvm/gems/ruby-2.6.3/gems/actionpack-5.0.7.2/lib/action_dispatch/middleware/templates/rescues/template_error.html.erb within rescues/layout (17.7ms)
該当のソースコード
app/model/User.rb
ruby
1class User < ApplicationRecord 2 has_many :microposts, dependent: :destroy 3| 4| 5end
app/model/Micropost.rb
ruby
1class Micropost < ApplicationRecord 2belongs_to :user 3| 4| 5end
class MicropostsController < ApplicationController def index @microposts=Micropost.all end end
/app/view/microposts/index.html.erb
ruby
1<%@microposts.each do |post|%> 2 <%= render "microposts/micropost" %> 3<%end%> 4 <%= paginate @microposts %>
/app/view/microposts/_micropost.html.erb
ruby
1<% micropost = Micropost %> 2<li id="micropost-<%= micropost.id %>"> 3 <%= link_to gravatar_for(micropost.user, size: 50), micropost.user %> 4 <span class="user"><%= link_to micropost.user.name, micropost.user %></span> 5 <span class="content"><%= micropost.content %></span> 6 <span class="timestamp"> 7 <%= time_ago_in_words(micropost.created_at)%> 前 8 </span> 9</li>
試したこと
binding.pryを使ってコンソール内でいくつか検証してみました。
Micropost.first.user
⇒ユーザー参照できました。
micropost.first.user
も同様です。
補足情報(FW/ツールのバージョンなど)
cloud9使用
Rails 5.0.7.2
助けていただけるとありがたいです。お願いします。
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/07/22 06:21
2019/07/22 06:48
2019/07/22 13:18
2019/07/23 00:20
2019/07/23 04:46