teratail header banner
teratail header banner
質問するログイン新規登録

質問編集履歴

1

indexのviewを追記しました!お願いします。

2021/08/25 00:54

投稿

YugoTokonami
YugoTokonami

スコア21

title CHANGED
File without changes
body CHANGED
@@ -6,18 +6,17 @@
6
6
 
7
7
 
8
8
  ### 発生している問題・エラーメッセージ
9
+
9
10
  NoMethodError in Articles#show
10
11
  Showing /Users/yukahayuusatoru/projects/b_blog/app/views/articles/show.html.erb where line #8 raised:
11
12
  undefined method `title' for nil:NilClass
12
13
  と表示されています。
13
14
 
14
- エラーメッセージ
15
- ```
16
15
 
17
16
 
18
17
  ### app/controllers/article_controller.rb
19
18
 
20
- class ArticlesController < ApplicationController
19
+ ```class ArticlesController < ApplicationController
21
20
 
22
21
  def index
23
22
  @articles = Article.all
@@ -47,10 +46,11 @@
47
46
  end
48
47
 
49
48
  end
49
+ ```
50
50
 
51
51
  ### app/views/articles/show.html.erb
52
-
52
+ ```
53
- <div class="header-title">
53
+ <div class="header-title">
54
54
  <%= link_to 'B-blog', root_path, class:"form-header-text" %>
55
55
  </div>
56
56
 
@@ -77,12 +77,51 @@
77
77
  </div>
78
78
  </div>
79
79
 
80
+ ```
81
+ ### app/views/articles/index.html.erb
82
+ ```
83
+ <div class="title">
84
+ <ul class="top-botton">
85
+ <% if user_signed_in? %>
86
+ <li><%= link_to current_user.name, "#", class:"user-name" %></li>
87
+ <li><%= link_to 'New Article', new_article_path, class:"new-article" %></li>
88
+ <%# <li><%= link_to 'ログアウト', destroy_user_session_path, method: :delete, class:"logout"%></li>
89
+ <% else %>
90
+ <li><%= link_to 'ログイン', new_user_session_path, class: "login" %></li>
91
+ <li><%= link_to '新規登録', new_user_registration_path, class: "sign-up" %>
92
+ <% end %>
93
+ </ul>
94
+ <div class="title-content">B-blog</div>
95
+ </div>
80
96
 
97
+ <div class="main">
98
+ <% @articles.each do |article| %>
99
+ <li class="list">
100
+ <%= link_to article_path(article.id), class:"article-name", method: :get do %>
101
+ <div class="article-img-content">
102
+ <%= image_tag article.image, class:"article-img"%>
103
+ </div>
104
+ <div class="article-info">
105
+ <h1 class="article-name">
106
+ <%= article.title%>
107
+ </h1>
108
+ <h1 class="article-name">
109
+ <%= article.category.name%>
110
+ </h1>
111
+ </div>
112
+ <% end %>
113
+ </li>
114
+ <% end %>
115
+ </div>
81
116
 
117
+ ```
118
+ 追記でindex.html.erbのビューファイルも載せます。
82
119
 
83
120
 
84
121
 
85
122
 
123
+
124
+
86
125
  ### 試したこと
87
126
 
88
127
  予想として、エラーの内容は「@article.titleの@articleの部分がNilClassですよ」という意味なので、