質問編集履歴
1
編集・追加依頼の内容に従って情報を追記しました。
test
CHANGED
File without changes
|
test
CHANGED
@@ -14,11 +14,11 @@
|
|
14
14
|
|
15
15
|
|
16
16
|
|
17
|
-
|
17
|
+
投稿一覧ページの各投稿に、icon、nickname、user_nameを表示する方法を教えていただけませんでしょうか。
|
18
|
-
|
19
|
-
|
20
|
-
|
18
|
+
|
19
|
+
|
20
|
+
|
21
|
-
###
|
21
|
+
### 投稿一覧ページ
|
22
22
|
|
23
23
|
|
24
24
|
|
@@ -44,6 +44,30 @@
|
|
44
44
|
|
45
45
|
```
|
46
46
|
|
47
|
+
#top_controller
|
48
|
+
|
49
|
+
|
50
|
+
|
51
|
+
class TopController < ApplicationController
|
52
|
+
|
53
|
+
def home
|
54
|
+
|
55
|
+
@post = Post.all.order(created_at: :desc)
|
56
|
+
|
57
|
+
end
|
58
|
+
|
59
|
+
end
|
60
|
+
|
61
|
+
```
|
62
|
+
|
63
|
+
|
64
|
+
|
65
|
+
### 投稿詳細ページ
|
66
|
+
|
67
|
+
|
68
|
+
|
69
|
+
```
|
70
|
+
|
47
71
|
#show.html.erb こちらでは表示できる
|
48
72
|
|
49
73
|
|
@@ -60,6 +84,158 @@
|
|
60
84
|
|
61
85
|
<%= link_to(@user.user_name, "http://localhost:3000/users/#{@user.id}") %>
|
62
86
|
|
63
|
-
|
87
|
+
</div>
|
64
|
-
|
88
|
+
|
65
|
-
```
|
89
|
+
```
|
90
|
+
|
91
|
+
|
92
|
+
|
93
|
+
```
|
94
|
+
|
95
|
+
#post_controller
|
96
|
+
|
97
|
+
|
98
|
+
|
99
|
+
class PostController < ApplicationController
|
100
|
+
|
101
|
+
protect_from_forgery except: :update
|
102
|
+
|
103
|
+
before_action :ensure_correct_user,{only: [:edit,:update,:destroy]}
|
104
|
+
|
105
|
+
|
106
|
+
|
107
|
+
|
108
|
+
|
109
|
+
def show
|
110
|
+
|
111
|
+
@id = params[:id]
|
112
|
+
|
113
|
+
@post = Post.find_by(id: params[:id])
|
114
|
+
|
115
|
+
@user = User.find_by(id: @post.user_id)
|
116
|
+
|
117
|
+
end
|
118
|
+
|
119
|
+
|
120
|
+
|
121
|
+
def edit
|
122
|
+
|
123
|
+
@post = Post.find_by(id: params[:id])
|
124
|
+
|
125
|
+
end
|
126
|
+
|
127
|
+
|
128
|
+
|
129
|
+
def update
|
130
|
+
|
131
|
+
@post = Post.find_by(id: params[:id])
|
132
|
+
|
133
|
+
@post.content = params[:content]
|
134
|
+
|
135
|
+
@post.save
|
136
|
+
|
137
|
+
redirect_to("/")
|
138
|
+
|
139
|
+
end
|
140
|
+
|
141
|
+
|
142
|
+
|
143
|
+
def destroy
|
144
|
+
|
145
|
+
@post = Post.find_by(id: params[:id])
|
146
|
+
|
147
|
+
@post.destroy
|
148
|
+
|
149
|
+
redirect_to("/")
|
150
|
+
|
151
|
+
end
|
152
|
+
|
153
|
+
|
154
|
+
|
155
|
+
def ensure_correct_user
|
156
|
+
|
157
|
+
@post = Post.find_by(id:params[:id])
|
158
|
+
|
159
|
+
if @post.user_id != @current_user.id
|
160
|
+
|
161
|
+
flash[:notice] = "権限がありません"
|
162
|
+
|
163
|
+
redirect_to("/posts/index")
|
164
|
+
|
165
|
+
end
|
166
|
+
|
167
|
+
end
|
168
|
+
|
169
|
+
|
170
|
+
|
171
|
+
end
|
172
|
+
|
173
|
+
|
174
|
+
|
175
|
+
```
|
176
|
+
|
177
|
+
|
178
|
+
|
179
|
+
### post model 投稿テーブル
|
180
|
+
|
181
|
+
```
|
182
|
+
|
183
|
+
#post.rb
|
184
|
+
|
185
|
+
class Post < ApplicationRecord
|
186
|
+
|
187
|
+
end
|
188
|
+
|
189
|
+
```
|
190
|
+
|
191
|
+
|
192
|
+
|
193
|
+
### User model
|
194
|
+
|
195
|
+
|
196
|
+
|
197
|
+
```
|
198
|
+
|
199
|
+
#user.rb ユーザーテーブル
|
200
|
+
|
201
|
+
|
202
|
+
|
203
|
+
class User < ApplicationRecord
|
204
|
+
|
205
|
+
# Include default devise modules. Others available are:
|
206
|
+
|
207
|
+
# :confirmable, :lockable, :timeoutable, :trackable and :omniauthable
|
208
|
+
|
209
|
+
devise :database_authenticatable, :registerable,
|
210
|
+
|
211
|
+
:recoverable, :rememberable, :validatable
|
212
|
+
|
213
|
+
|
214
|
+
|
215
|
+
before_create{ self.nickname = username }
|
216
|
+
|
217
|
+
|
218
|
+
|
219
|
+
def user_name
|
220
|
+
|
221
|
+
"@#{self.username}"
|
222
|
+
|
223
|
+
end
|
224
|
+
|
225
|
+
|
226
|
+
|
227
|
+
def posts
|
228
|
+
|
229
|
+
return Post.where(user_id: self.id)
|
230
|
+
|
231
|
+
end
|
232
|
+
|
233
|
+
|
234
|
+
|
235
|
+
mount_uploader :icon, ImageUploader
|
236
|
+
|
237
|
+
|
238
|
+
|
239
|
+
end
|
240
|
+
|
241
|
+
```
|