質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Ruby

Rubyはプログラミング言語のひとつで、オープンソース、オブジェクト指向のプログラミング開発に対応しています。

Q&A

解決済

1回答

1359閲覧

railsでアプリ作成、アソシエーションの問題かと思います。

banianizm

総合スコア92

Ruby

Rubyはプログラミング言語のひとつで、オープンソース、オブジェクト指向のプログラミング開発に対応しています。

0グッド

0クリップ

投稿2017/08/01 22:03

お世話になります。

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

よろしくおねがいいたします。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

作成者名の入っていないTopicが混入している、ということではないでしょうか。

リレーションが行方不明のデータを作りたくない場合、

  1. データベースレベルでFOREIGN KEYNOT NULLをかけて、関連するデータだけ入れられるようにする
  2. Railsでvalidates inclusion:を使って、関連したデータを要求する

など、チェックが可能です。

投稿2017/08/01 22:38

maisumakun

総合スコア145183

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問