質問編集履歴

4

情報を増やしました

2019/08/31 05:31

投稿

Yuki0531-
Yuki0531-

スコア4

test CHANGED
File without changes
test CHANGED
@@ -250,14 +250,6 @@
250
250
 
251
251
 
252
252
 
253
- ```
254
-
255
- <% @posts.each do |post| %>
256
-
257
- ```
258
-
259
-
260
-
261
253
  ### 試したこと
262
254
 
263
255
 
@@ -268,6 +260,12 @@
268
260
 
269
261
 
270
262
 
263
+ テラテイル内でこれに似たエラーの解決方法を見つけ、実戦しましたが、これも上手くいきませんでした。
264
+
265
+   =>データベースが壊れているかもしれないので、データを削除してみる。
266
+
267
+
268
+
271
269
  以下のようなエラーが出た時は、どこを修正すればいいのでしょうか…
272
270
 
273
271
 

3

エラーが変わりましたので、更新します。

2019/08/31 05:31

投稿

Yuki0531-
Yuki0531-

スコア4

test CHANGED
File without changes
test CHANGED
@@ -20,9 +20,7 @@
20
20
 
21
21
 
22
22
 
23
- undefined method `user' for #<Post:0x00007f8a228cc8b0>
23
+ undefined method `image_name' for nil:NilClass
24
-
25
- Did you mean? users
26
24
 
27
25
 
28
26
 
@@ -274,6 +272,4 @@
274
272
 
275
273
 
276
274
 
277
- undefined method `user' for #<Post:0x00007f8a228cc8b0>
275
+ undefined method `image_name' for nil:NilClass
278
-
279
- Did you mean? users

2

情報の追加

2019/08/31 05:28

投稿

Yuki0531-
Yuki0531-

スコア4

test CHANGED
File without changes
test CHANGED
@@ -24,7 +24,9 @@
24
24
 
25
25
  Did you mean? users
26
26
 
27
+
28
+
27
- around line #6 => 6行目にエラーができています。
29
+  => 3行目と6行目にエラーができています。
28
30
 
29
31
 
30
32
 
@@ -36,13 +38,13 @@
36
38
 
37
39
  <div class="container">
38
40
 
39
- <% @posts.each do |post| %>
41
+ <% @posts.each do |post| %>  =>ここにエラー
40
42
 
41
43
  <div class="posts-index-item">
42
44
 
43
45
  <div class="post-left">
44
46
 
45
- <img src="<%= "/user_images/#{post.user.image_name}" %>">
47
+ <img src="<%= "/user_images/#{post.user.image_name}" %>">  =>ここにエラー
46
48
 
47
49
  </div>
48
50
 

1

情報を追加しております。

2019/08/31 03:13

投稿

Yuki0531-
Yuki0531-

スコア4

test CHANGED
File without changes
test CHANGED
@@ -24,16 +24,230 @@
24
24
 
25
25
  Did you mean? users
26
26
 
27
-
27
+ around line #6 => 6行目にエラーができています。
28
-
28
+
29
+
30
+
31
+
32
+
29
- ```
33
+ ```
34
+
30
-
35
+ <div class="main posts-index">
36
+
37
+ <div class="container">
38
+
39
+ <% @posts.each do |post| %>
40
+
41
+ <div class="posts-index-item">
42
+
43
+ <div class="post-left">
44
+
31
- <img src="<%= "/user_images/#{post.user.image_name}" %>">
45
+ <img src="<%= "/user_images/#{post.user.image_name}" %>">
46
+
47
+ </div>
48
+
49
+ <div class="post-right">
50
+
51
+ <div class="post-user-name">
52
+
53
+ <%= link_to(post.user.name, "/users/#{post.user.id}") %>
54
+
55
+ </div>
56
+
57
+ <%= link_to(post.content, "/posts/#{post.id}") %>
58
+
59
+ </div>
60
+
61
+ </div>
62
+
63
+ <% end %>
64
+
65
+ </div>
66
+
67
+ </div>
68
+
69
+
32
70
 
33
71
  ```
34
72
 
35
73
  
36
74
 
75
+ controller/post_controller.rbにて
76
+
77
+
78
+
79
+
80
+
81
+ ```
82
+
83
+ class PostsController < ApplicationController
84
+
85
+ before_action :authenticate_user
86
+
87
+ before_action :ensure_correct_user, {only: [:edit, :update, :destroy]}
88
+
89
+
90
+
91
+ def index
92
+
93
+ @posts = Post.all.order(created_at: :desc)
94
+
95
+ end
96
+
97
+
98
+
99
+ def show
100
+
101
+ @post = Post.find_by(id: params[:id])
102
+
103
+ @user = User.find_by(id: @post.user_id)
104
+
105
+ end
106
+
107
+ def new
108
+
109
+ @post = Post.new
110
+
111
+ end
112
+
113
+
114
+
115
+ def create
116
+
117
+ @post = Post.new(
118
+
119
+ content: params[:content],
120
+
121
+ user_id: @current_user.id
122
+
123
+ )
124
+
125
+ if @post.save
126
+
127
+ flash[:notice] = "Post successfully created"
128
+
129
+ redirect_to("/posts/index")
130
+
131
+ else
132
+
133
+ render("posts/new")
134
+
135
+ end
136
+
137
+ end
138
+
139
+
140
+
141
+ def edit
142
+
143
+ @post = Post.find_by(id: params[:id])
144
+
145
+ end
146
+
147
+
148
+
149
+ def update
150
+
151
+
152
+
153
+ @post = Post.find_by(id: params[:id])
154
+
155
+ @post.content = params[:content]
156
+
157
+
158
+
159
+ if @post.save
160
+
161
+ flash[:notice] = "Post successfully edited"
162
+
163
+ redirect_to("/posts/index")
164
+
165
+ else
166
+
167
+ render("posts/edit")
168
+
169
+ end
170
+
171
+ end
172
+
173
+
174
+
175
+ def destroy
176
+
177
+ @post = Post.find_by(id: params[:id])
178
+
179
+ @post.destroy
180
+
181
+ flash[:notice] = "Post successfully deleted"
182
+
183
+ redirect_to("/posts/index")
184
+
185
+ end
186
+
187
+
188
+
189
+ def ensure_correct_user
190
+
191
+ @post = Post.find_by(id: params[:id])
192
+
193
+ if @post.user_id != @current_user.id
194
+
195
+ flash[:notice] = "Unauthorized access"
196
+
197
+ redirect_to("/posts/index")
198
+
199
+ end
200
+
201
+ end
202
+
203
+
204
+
205
+ end
206
+
207
+
208
+
209
+
210
+
211
+ ```
212
+
213
+
214
+
215
+ model/post.rbにて
216
+
217
+
218
+
219
+
220
+
221
+ ```
222
+
223
+ class Post < ApplicationRecord
224
+
225
+ validates :content, {presence: true, length: {maximum: 140}}
226
+
227
+ validates :user_id, {presence: true}
228
+
229
+
230
+
231
+ def users
232
+
233
+ return User.find_by(id: self.user._id)
234
+
235
+ end
236
+
237
+
238
+
239
+
240
+
241
+ end
242
+
243
+
244
+
245
+ ```
246
+
247
+
248
+
249
+
250
+
37
251
 
38
252
 
39
253
  ```