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

質問編集履歴

1

書式の改善

2018/04/18 06:38

投稿

yaegashi0852
yaegashi0852

スコア13

title CHANGED
File without changes
body CHANGED
@@ -6,28 +6,25 @@
6
6
  投稿一覧(index.html.erb)にユーザーの画像を表示しようとしたらエラー発生。
7
7
 
8
8
  ### 発生している問題・エラーメッセージ
9
+ ```
9
- NameError in Posts#index
10
+ NoMethodError in Posts#index
10
11
  Showing /vagrant/new_app/app/views/posts/index.html.erb where line #6 raised:
11
12
 
12
- undefined local variable or method `user' for #<#<Class:0xb647d34c>:0xb51a2fd8>
13
+ undefined method `image_name' for nil:NilClass
13
14
  Extracted source (around line #6):
15
+
14
- 4 <div class="posts-index-item">
16
+ 4 <div class="posts-index-item">
15
- 5 <div class="post-left">
17
+ 5 <div class="post-left">
16
- 6  <img src="<%= "/user_images/#{user.image_name}" %>">
18
+ 6 <img src="<%= "/post.user_images/#{post.user.image_name}" %>">
17
- 7  </div>
19
+ 7 </div>
18
- 8 <div class="post-right">
20
+ 8 <div class="post-right">
19
- 9  <div class="post-user-name">
21
+ 9 <div class="post-user-name">
20
22
 
21
-
22
-
23
-
24
-
25
-
26
23
  Rails.root: /vagrant/new_app
27
24
 
28
25
  Application Trace | Framework Trace | Full Trace
29
- app/views/posts/index.html.erb:6:in `block in _app_views_posts_index_html_erb__314821907__628286838'
26
+ app/views/posts/index.html.erb:6:in `block in _app_views_posts_index_html_erb___226630752__647422898'
30
- app/views/posts/index.html.erb:3:in `_app_views_posts_index_html_erb__314821907__628286838'
27
+ app/views/posts/index.html.erb:3:in `_app_views_posts_index_html_erb___226630752__647422898'
31
28
  Request
32
29
 
33
30
  Parameters:
@@ -40,10 +37,10 @@
40
37
  Headers:
41
38
 
42
39
  None
40
+ ```
43
41
 
42
+ ### 該当のソースコード(routes.rb)
44
43
 
45
- ### 該当のソースコード
46
-
47
44
  ```Ruby
48
45
  Rails.application.routes.draw do
49
46
  get "login" => "users#login_form"
@@ -69,21 +66,9 @@
69
66
  get "about" => "home#about"
70
67
  end
71
68
  ```
72
- ### 該当のソースコード
73
69
 
74
- ```Ruby
75
- class Post < ApplicationRecord
70
+ ### 該当のソースコード(posts_controller.rb)
76
- validates :content, {presence: true, length: {maximum: 140}}
77
- validates :user_id, {presence: true}
78
71
 
79
- def user
80
- return User.find_by(id: self.user_id)
81
- end
82
-
83
- end
84
- ```
85
- ### 該当のソースコード
86
-
87
72
  ```Ruby
88
73
  class PostsController < ApplicationController
89
74
 
@@ -140,7 +125,7 @@
140
125
  end
141
126
  ```
142
127
 
143
- ### 該当のソースコード
128
+ ### 該当のソースコード(index.html.erb)
144
129
 
145
130
  ```HTML
146
131
  <div class="main posts-index">
@@ -148,7 +133,7 @@
148
133
  <% @posts.each do |post| %>
149
134
  <div class="posts-index-item">
150
135
  <div class="post-left">
151
- <img src="<%= "/post.user_images/#{user.image_name}" %>">
136
+ <img src="<%= "/post.user_images/#{post.user.image_name}" %>">
152
137
  </div>
153
138
  <div class="post-right">
154
139
  <div class="post-user-name">
@@ -162,6 +147,52 @@
162
147
  </div>
163
148
  ```
164
149
 
150
+ ### 該当のソースコード(show.html.erb)
151
+ ```HTML
152
+ <div class="main posts-show">
153
+ <div class="container">
154
+ <div class="posts-show-item">
155
+ <div class="post-user-name">
156
+ <img src="<%= "/user_images/#{@user.image_name}" %>">
157
+ <%= link_to(@user.name, "/users/#{@user.id}") %>
158
+ </div>
159
+ <p>
160
+ <%= @post.content %>
161
+ </p>
162
+ <div class="post-time">
163
+ <%= @post.created_at %>
164
+ </div>
165
+ <div class="post-menus">
166
+ <%= link_to("編集", "/posts/#{@post.id}/edit") %>
167
+ <%= link_to("削除", "/posts/#{@post.id}/destroy",{method: "post"}) %>
168
+ </div>
169
+ </div>
170
+ </div>
171
+ </div>
172
+ ```
173
+
174
+ ### 該当のソースコード(user.rb)
175
+ ```Ruby
176
+ class User < ApplicationRecord
177
+ validates :name,{presence: true}
178
+ validates :email, {presence: true, uniqueness: true}
179
+ validates :password, {presence: true}
180
+ end
181
+ ```
182
+
183
+
184
+ ### 該当のソースコード(post.rb)
185
+ ```Ruby
186
+ class Post < ApplicationRecord
187
+ validates :content, {presence: true, length: {maximum: 140}}
188
+ validates :user_id, {presence: true}
189
+
190
+ def user
191
+ return User.find_by(id: self.user_id)
192
+ end
193
+
194
+ end
195
+ ```
165
196
  ### 試したこと
166
-
197
+ 投稿の詳細画面を表示しようとしたところそれでもメソッドエラーが出たので
167
- 解答のコピ&ペー
198
+ ビュ以外にミがあるのかと思いました。