お世話になります。
railsでアプリ作成しています。
トピック一覧で投稿者を表示しようと考えて
topics/index.html.erbで以下のコードを記述しました。
<div class="container"> <div class="wrapper col-md-8 col-md-offset-2 col-sm-10"> <p><%= notice %></p> <h1>トピック一覧</h1> <%= link_to "トピックを作成する", new_topic_path, class: 'btn btn-default' %> <% @topics.each do |topic| %> <div class="row"> <%= topic.user.name %>が投稿しました。 <%= link_to topic_path(topic) do %> <h4><strong><%= topic.title %></strong></h4> <% end %> <p><%= topic.content %></p> <%= link_to "編集", edit_topic_path(topic.id), class: 'btn btn-default btn-sm btn-success' %> <%= link_to "削除", topic_path(topic.id), method: :delete, data: { confirm: '本当に削除していいですか?'}, class: 'btn btn-default btn-sm btn btn-danger' %> </div> <% end %> </div> </div>
本番環境でデプロイして、投稿者一覧ページを表示しようとすると
以下のエラーが出ます。
Rendered topics/index.html.erb within layouts/application (12.8ms) Completed 500 Internal Server Error in 17ms (ActiveRecord: 1.7ms) ActionView::Template::Error (undefined method `name' for nil:NilClass): 5: <%= link_to "トピックを作成する", new_topic_path, class: 'btn btn-default' %> 6: <% @topics.each do |topic| %> 7: <div class="row"> 8: <%= topic.user.name %>が投稿しました。 9: <%= link_to topic_path(topic) do %> 10: <h4><strong><%= topic.title %></strong></h4> 11: <% end %> app/views/topics/index.html.erb:8:in `block in _app_views_topics_index_html_erb__3061023843254581377_47145674463940' app/views/topics/index.html.erb:6:in `_app_views_topics_index_html_erb__3061023843254581377_47145674463940' 124.213.168.66 - - [02/Aug/2017:06:09:14 +0900] "GET /500 HTTP/1.0" 500 533 0.0217 [app@ip-172-31-29-95 ~]$
アソシエーションの問題かと考えて確認しました。
models/user.rbファイル
class User < ActiveRecord::Base devise :database_authenticatable, :registerable, :recoverable, :rememberable, :trackable, :validatable, :confirmable, :omniauthable mount_uploader :avatar, AvatarUploader has_many :topics, dependent: :destroy # CommentモデルのAssociationを設定 has_many :comments, dependent: :destroy has_many :relationships, foreign_key: "follower_id", dependent: :destroy has_many :reverse_relationships, foreign_key: "followed_id", class_name: "Relationship", dependent: :destroy has_many :followed_users, through: :relationships, source: :followed has_many :followers, through: :reverse_relationships, source: :follower def self.find_for_facebook_oauth(auth, signed_in_resource=nil) user = User.find_by(email: auth.info.email) unless user user = User.new( name: auth.extra.raw_info.name, provider: auth.provider, uid: auth.uid, email: auth.info.email ||= "#{auth.uid}-#{auth.provider}@example.com", image_url: auth.info.image, password: Devise.friendly_token[0, 20] ) user.skip_confirmation! user.save(validate: false) end user end def self.find_for_twitter_oauth(auth, signed_in_resource = nil) user = User.find_by(provider: auth.provider, uid: auth.uid) unless user user = User.new( name: auth.info.nickname, image_url: auth.info.image, provider: auth.provider, uid: auth.uid, email: auth.info.email ||= "#{auth.uid}-#{auth.provider}@example.com", password: Devise.friendly_token[0, 20] ) user.skip_confirmation! user.save end user end def self.create_unique_string SecureRandom.uuid end def update_with_password(params, *options) if provider.blank? super else params.delete :current_password update_without_password(params, *options) end end def follow!(other_user) relationships.create!(followed_id: other_user.id) end def unfollow!(other_user) relationships.find_by(followed_id: other_user.id).destroy end #フォローしているかどうかを確認する def following?(other_user) relationships.find_by(followed_id: other_user.id) end end
こちらも問題ないと思います。
models/topic.rbファイルです。
class Topic < ActiveRecord::Base validates :title, presence: true belongs_to :user has_many :comments, dependent: :destroy end
他に考えられる原因はなんでしょうか?
githubのurlです。
https://github.com/bani-m/face_sns.git
よろしくおねがいいたします。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。