質問編集履歴
4
情報を増やしました
title
CHANGED
File without changes
|
body
CHANGED
@@ -124,15 +124,14 @@
|
|
124
124
|
|
125
125
|
|
126
126
|
|
127
|
-
```
|
128
|
-
<% @posts.each do |post| %>
|
129
|
-
```
|
130
|
-
|
131
127
|
### 試したこと
|
132
128
|
|
133
129
|
Progateのコードを全てそのまま模写したり、時にはコピペをしました。
|
134
130
|
しかし、うまくいきませんでした。
|
135
131
|
|
132
|
+
テラテイル内でこれに似たエラーの解決方法を見つけ、実戦しましたが、これも上手くいきませんでした。
|
133
|
+
=>データベースが壊れているかもしれないので、データを削除してみる。
|
134
|
+
|
136
135
|
以下のようなエラーが出た時は、どこを修正すればいいのでしょうか…
|
137
136
|
|
138
137
|
undefined method `image_name' for nil:NilClass
|
3
エラーが変わりましたので、更新します。
title
CHANGED
File without changes
|
body
CHANGED
@@ -9,8 +9,7 @@
|
|
9
9
|
|
10
10
|
app/views/posts/index.html.erbのページにて
|
11
11
|
|
12
|
-
|
12
|
+
undefined method `image_name' for nil:NilClass
|
13
|
-
Did you mean? users
|
14
13
|
|
15
14
|
=> 3行目と6行目にエラーができています。
|
16
15
|
|
@@ -136,5 +135,4 @@
|
|
136
135
|
|
137
136
|
以下のようなエラーが出た時は、どこを修正すればいいのでしょうか…
|
138
137
|
|
139
|
-
|
138
|
+
undefined method `image_name' for nil:NilClass
|
140
|
-
Did you mean? users
|
2
情報の追加
title
CHANGED
File without changes
|
body
CHANGED
@@ -11,16 +11,17 @@
|
|
11
11
|
|
12
12
|
undefined method `user' for #<Post:0x00007f8a228cc8b0>
|
13
13
|
Did you mean? users
|
14
|
-
around line #6 => 6行目にエラーができています。
|
15
14
|
|
15
|
+
=> 3行目と6行目にエラーができています。
|
16
16
|
|
17
|
+
|
17
18
|
```
|
18
19
|
<div class="main posts-index">
|
19
20
|
<div class="container">
|
20
|
-
<% @posts.each do |post| %>
|
21
|
+
<% @posts.each do |post| %> =>ここにエラー
|
21
22
|
<div class="posts-index-item">
|
22
23
|
<div class="post-left">
|
23
|
-
<img src="<%= "/user_images/#{post.user.image_name}" %>">
|
24
|
+
<img src="<%= "/user_images/#{post.user.image_name}" %>"> =>ここにエラー
|
24
25
|
</div>
|
25
26
|
<div class="post-right">
|
26
27
|
<div class="post-user-name">
|
1
情報を追加しております。
title
CHANGED
File without changes
|
body
CHANGED
@@ -11,13 +11,120 @@
|
|
11
11
|
|
12
12
|
undefined method `user' for #<Post:0x00007f8a228cc8b0>
|
13
13
|
Did you mean? users
|
14
|
+
around line #6 => 6行目にエラーができています。
|
14
15
|
|
16
|
+
|
15
17
|
```
|
18
|
+
<div class="main posts-index">
|
19
|
+
<div class="container">
|
20
|
+
<% @posts.each do |post| %>
|
21
|
+
<div class="posts-index-item">
|
22
|
+
<div class="post-left">
|
16
|
-
|
23
|
+
<img src="<%= "/user_images/#{post.user.image_name}" %>">
|
24
|
+
</div>
|
25
|
+
<div class="post-right">
|
26
|
+
<div class="post-user-name">
|
27
|
+
<%= link_to(post.user.name, "/users/#{post.user.id}") %>
|
28
|
+
</div>
|
29
|
+
<%= link_to(post.content, "/posts/#{post.id}") %>
|
30
|
+
</div>
|
31
|
+
</div>
|
32
|
+
<% end %>
|
33
|
+
</div>
|
34
|
+
</div>
|
35
|
+
|
17
36
|
```
|
18
37
|

|
38
|
+
controller/post_controller.rbにて
|
19
39
|
|
40
|
+
|
20
41
|
```
|
42
|
+
class PostsController < ApplicationController
|
43
|
+
before_action :authenticate_user
|
44
|
+
before_action :ensure_correct_user, {only: [:edit, :update, :destroy]}
|
45
|
+
|
46
|
+
def index
|
47
|
+
@posts = Post.all.order(created_at: :desc)
|
48
|
+
end
|
49
|
+
|
50
|
+
def show
|
51
|
+
@post = Post.find_by(id: params[:id])
|
52
|
+
@user = User.find_by(id: @post.user_id)
|
53
|
+
end
|
54
|
+
def new
|
55
|
+
@post = Post.new
|
56
|
+
end
|
57
|
+
|
58
|
+
def create
|
59
|
+
@post = Post.new(
|
60
|
+
content: params[:content],
|
61
|
+
user_id: @current_user.id
|
62
|
+
)
|
63
|
+
if @post.save
|
64
|
+
flash[:notice] = "Post successfully created"
|
65
|
+
redirect_to("/posts/index")
|
66
|
+
else
|
67
|
+
render("posts/new")
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
def edit
|
72
|
+
@post = Post.find_by(id: params[:id])
|
73
|
+
end
|
74
|
+
|
75
|
+
def update
|
76
|
+
|
77
|
+
@post = Post.find_by(id: params[:id])
|
78
|
+
@post.content = params[:content]
|
79
|
+
|
80
|
+
if @post.save
|
81
|
+
flash[:notice] = "Post successfully edited"
|
82
|
+
redirect_to("/posts/index")
|
83
|
+
else
|
84
|
+
render("posts/edit")
|
85
|
+
end
|
86
|
+
end
|
87
|
+
|
88
|
+
def destroy
|
89
|
+
@post = Post.find_by(id: params[:id])
|
90
|
+
@post.destroy
|
91
|
+
flash[:notice] = "Post successfully deleted"
|
92
|
+
redirect_to("/posts/index")
|
93
|
+
end
|
94
|
+
|
95
|
+
def ensure_correct_user
|
96
|
+
@post = Post.find_by(id: params[:id])
|
97
|
+
if @post.user_id != @current_user.id
|
98
|
+
flash[:notice] = "Unauthorized access"
|
99
|
+
redirect_to("/posts/index")
|
100
|
+
end
|
101
|
+
end
|
102
|
+
|
103
|
+
end
|
104
|
+
|
105
|
+
|
106
|
+
```
|
107
|
+
|
108
|
+
model/post.rbにて
|
109
|
+
|
110
|
+
|
111
|
+
```
|
112
|
+
class Post < ApplicationRecord
|
113
|
+
validates :content, {presence: true, length: {maximum: 140}}
|
114
|
+
validates :user_id, {presence: true}
|
115
|
+
|
116
|
+
def users
|
117
|
+
return User.find_by(id: self.user._id)
|
118
|
+
end
|
119
|
+
|
120
|
+
|
121
|
+
end
|
122
|
+
|
123
|
+
```
|
124
|
+
|
125
|
+
|
126
|
+
|
127
|
+
```
|
21
128
|
<% @posts.each do |post| %>
|
22
129
|
```
|
23
130
|
|