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

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

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

Ruby on Railsは、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

Q&A

解決済

1回答

1167閲覧

Ruby on Rails: undefined method `user' for nil:NilClassのNoMethodErrorについて

Shoukenn

総合スコア6

Ruby on Rails

Ruby on Railsは、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

0グッド

0クリップ

投稿2020/10/05 01:38

show.html.erbにイイね機能とコメント機能を付けたのち、以下のようなエラーが出てしまいました。
イメージ説明
関連あるようなコードは以下のように乗っけていきます。

Ruby

1class User < ApplicationRecord 2 # Include default devise modules. Others available are: 3 # :confirmable, :lockable, :timeoutable, :trackable and :omniauthable 4 devise :database_authenticatable, :registerable, 5 :recoverable, :rememberable, :validatable 6 7 has_many :articles, dependent: :destroy 8 has_many :comments 9 has_many :comments, dependent: :destroy 10 has_many :likes 11 has_many :likes, dependent: :destroy 12 has_many :liked_articles, through: :likes, source: :article 13 14 def already_liked?(article) 15 self.likes.exists?(article_id: article.id) 16 end 17 18end 19

Ruby

1class Article < ApplicationRecord 2 belongs_to :user 3 has_many :likes, dependent: :destroy 4 has_many :liked_users, through: :likes, source: :user 5 has_many :comments, dependent: :destroy 6end 7

Ruby

1class Comment < ApplicationRecord 2 belongs_to :article 3 belongs_to :user 4end 5

Ruby

1class ArticlesController < ApplicationController 2 before_action :authenticate_user! 3 4 def index 5 @articles = Article.all 6 end 7 8 def new 9 @articles = Article.new 10 end 11 12 def create 13 @articles = Article.new(article_params) 14 15 @articles.user_id = current_user.id 16 17 if @articles.save 18 redirect_to :action => "index" 19 else 20 redirect_to :action => "new" 21 end 22 end 23 24 def show 25 @article = Article.find(params[:id]) 26 @like = Like.new 27 @comments = @article.comments 28 @comment = Comment.new 29 end 30 31 private 32 def article_params 33 params.require(:article).permit(:title,:content,:category) 34 end 35 36end 37

Ruby

1class LikesController < ApplicationController 2 def create 3 @like = current_user.likes.create(article_id: params[:article_id]) 4 redirect_back(fallback_location: root_path) 5 end 6 7 def destroy 8 @like = Like.find_by(article_id: params[:article_id], user_id: current_user.id) 9 @like.destroy 10 redirect_back(fallback_location: root_path) 11 end 12end

Ruby

1class CommentsController < ApplicationController 2 before_action :authenticate_user! 3 4 def create 5 article = Article.find(params[:article_id]) 6 comment = article.comments.build(comment_params) #buildを使い、contentとtweet_idの二つを同時に代入 7 comment.user_id = current_user.id 8 if comment.save 9 flash[:success] = "コメントしました" 10 redirect_back(fallback_location: root_path) 11 else 12 flash[:success] = "コメントできませんでした" 13 redirect_back(fallback_location: root_path) 14 end 15 end 16 17 private 18 19 def comment_params 20 params.require(:comment).permit(:content) 21 end 22end

問題が起きているshow.html.erb

Ruby

1<h4>し か く  ちゃんねる</h4> 2<h1>資格い頻道</h1> 3 4<div class="article"> 5 <p><%= @articles.user.email %></p> 6 <h3><%= @articles.content %></h3> 7 <p><%= @articles.created_at %></p> 8 9 <h3>いいね件数: <%= @articles.likes.count %></h3> 10 <% if current_user.already_liked?(@article) %> 11 <%=link_to article_like_path(@article), method: :delete do%> 12 <i class="fas fa-heart"></i> 13   <%end%> 14 <% else %> 15 <%=link_to article_likes_path(@article), method: :post do%> 16 <i class="fas fa-heart"></i> 17   <%end%> 18 <% end %> 19 20</div> 21 22<h2>いいねしたユーザー</h2> 23<% @article.liked_users.each do |user| %> 24 <li><%= user.email %></li> 25<% end %> 26 27<div class="comment-wrapper"> 28 <p>コメント一覧</p> 29 <% @comments.each do |c| %> 30 <div> 31 <%= c.user.email unless c.user.blank? %> 32 <br> 33 <%= c.content %> 34 </div> 35 <br> 36 <% end %> 37 38 <% if user_signed_in? %> 39 <%= form_with(model: [@article, @comment], local: true) do |f| %> 40 <%= f.text_area :content %> 41 <%= button_tag type: "submit" do %> 42 <i class="far fa-comments"></i> コメントする 43 <% end %> 44 <% end %> 45 <% end %> 46</div> 47 48<%= link_to "編集する", edit_article_path(@article.id) %> 49<%= link_to "一覧に戻る", articles_path %>

routes.rb

Ruby

1Rails.application.routes.draw do 2 devise_for :users 3 # For details on the DSL available within this file, see https://guides.rubyonrails.org/routing.html 4 5 resources :articles do 6 resources :likes, only: [:create, :destroy] 7 resources :comments, only: [:create] 8 end 9 root 'articles#index' 10 11 get 'articles/top' => 'article#top' 12 get 'articles/intro' => 'article#intro' 13 14end 15

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

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

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

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

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

guest

回答1

0

ベストアンサー

@articlesがnilになってしまっているため、`user'がないぞ!とおっしゃってます。

そこらへんをしらべてみては。

投稿2020/10/05 01:44

y_waiwai

総合スコア87749

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問